新增长连接 接收发送消息实现

This commit is contained in:
xia jun 2024-11-03 13:48:21 +08:00
parent 626ee06c21
commit 0d6ff67f08
11 changed files with 135 additions and 15 deletions

View File

@ -0,0 +1,83 @@
/*
* Copyright 2013-2022 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.component.handler;
import com.farsunset.cim.component.handler.annotation.CIMHandler;
import com.farsunset.cim.component.push.DefaultMessagePusher;
import com.farsunset.cim.component.redis.SignalRedisTemplate;
import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.constants.Constants;
import com.farsunset.cim.entity.Session;
import com.farsunset.cim.group.SessionGroup;
import com.farsunset.cim.handler.CIMRequestHandler;
import com.farsunset.cim.model.Message;
import com.farsunset.cim.model.ReplyBody;
import com.farsunset.cim.model.SentBody;
import com.farsunset.cim.service.SessionService;
import io.netty.channel.Channel;
import org.springframework.http.HttpStatus;
import javax.annotation.Resource;
/**
* 客户端长连接通道发消息
*/
@CIMHandler(key = "client_message")
public class MessageHandler implements CIMRequestHandler {
@Resource
private DefaultMessagePusher defaultMessagePusher;
@Override
public void process(Channel channel, SentBody body) {
Message message = new Message();
/*
获取到当前链接的UID
*/
message.setSender(channel.attr(ChannelAttr.UID).get());
message.setReceiver(body.get("receiver"));
message.setAction(body.get("action"));
message.setContent(body.get("content"));
message.setFormat(body.get("format"));
message.setTitle(body.get("title"));
message.setExtra(body.get("extra"));
message.setId(System.currentTimeMillis());
defaultMessagePusher.push(message);
/*
* 将发送的消息ID 通过Replay异步发送给客户端
* 可通过reqId来对应上多个消息的各自ID
*/
ReplyBody reply = new ReplyBody();
reply.setKey(body.getKey());
reply.setCode(HttpStatus.OK.value());
reply.put("messageId",String.valueOf(message.getId()));
reply.put("requestId",body.get("requestId"));
reply.setTimestamp(System.currentTimeMillis());
}
}

View File

@ -39,26 +39,39 @@ import java.util.List;
public class SwaggerConfig {
@Bean
public Docket userApiDocket(ApiInfo apiInfo) {
public Docket messageApiDocket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo)
.groupName("客户端接口")
.apiInfo(new ApiInfoBuilder()
.title("CIM Push Service APIs.")
.description("消息发送相关接口")
.version("3.0")
.build())
.groupName("1、消息相关接口")
.select()
.apis(RequestHandlerSelectors.basePackage("com.farsunset.cim.mvc.controller.api"))
.apis(RequestHandlerSelectors.basePackage("com.farsunset.cim.mvc.controller.message"))
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("CIM Push Service APIs.")
.description("CIM客户端接口文档")
.version("3.0")
.build();
public Docket webrtcApiDocket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(new ApiInfoBuilder()
.title("CIM Push Service APIs.")
.description("可用于webrtc通话信令接口")
.version("3.0")
.build())
.groupName("2、Webrtc相关接口")
.select()
.apis(RequestHandlerSelectors.basePackage("com.farsunset.cim.mvc.controller.webrtc"))
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
private List<SecurityScheme> securitySchemes() {
List<SecurityScheme> schemeList = new ArrayList<>();
schemeList.add(new ApiKey("access-token", "access-token", "header"));

View File

@ -21,11 +21,13 @@
*/
package com.farsunset.cim.mvc.controller.admin;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@Api(produces = "application/json", tags = "页面导航",hidden = true)
public class NavigationController {
@GetMapping(value = "/")

View File

@ -22,6 +22,7 @@
package com.farsunset.cim.mvc.controller.admin;
import com.farsunset.cim.service.SessionService;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@ -30,6 +31,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
@Controller
@Api(produces = "application/json", tags = "在线用户页面",hidden = true)
@RequestMapping("/console/session")
public class SessionController {

View File

@ -19,7 +19,7 @@
* *
***************************************************************************************
*/
package com.farsunset.cim.mvc.controller.api;
package com.farsunset.cim.mvc.controller.message;
import com.farsunset.cim.service.SessionService;
import io.swagger.annotations.Api;

View File

@ -19,7 +19,7 @@
* *
***************************************************************************************
*/
package com.farsunset.cim.mvc.controller.api;
package com.farsunset.cim.mvc.controller.message;
import com.farsunset.cim.component.push.DefaultMessagePusher;
import com.farsunset.cim.model.Message;

View File

@ -19,7 +19,7 @@
* *
***************************************************************************************
*/
package com.farsunset.cim.mvc.controller.api;
package com.farsunset.cim.mvc.controller.webrtc;
import com.farsunset.cim.annotation.AccessToken;

View File

@ -19,7 +19,7 @@
* *
***************************************************************************************
*/
package com.farsunset.cim.mvc.controller.api;
package com.farsunset.cim.mvc.controller.webrtc;
import com.farsunset.cim.annotation.UID;
import com.farsunset.cim.component.push.DefaultMessagePusher;

View File

@ -45,7 +45,7 @@ public class TokenInterceptor implements HandlerInterceptor {
private AccessTokenService accessTokenService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String token = request.getHeader(HEADER_TOKEN);

View File

@ -83,6 +83,10 @@ public class ReplyBody implements Serializable, Transportable {
data.put(k, v);
}
public void put(String k, Number v) {
data.put(k, v.toString());
}
public String get(String k) {
return data.get(k);
}

View File

@ -61,6 +61,22 @@ public class SentBody implements Serializable {
return data.getOrDefault(key,defaultValue);
}
public long getLong(String key,long defaultValue) {
try {
return Long.parseLong(get(key));
} catch (final NumberFormatException nfe) {
return defaultValue;
}
}
public int getInt(String key,int defaultValue) {
try {
return Integer.parseInt(get(key));
} catch (final NumberFormatException nfe) {
return defaultValue;
}
}
public long getTimestamp() {
return timestamp;
}