mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-27 02:20:32 +08:00
新增长连接 接收发送消息实现
This commit is contained in:
parent
626ee06c21
commit
0d6ff67f08
@ -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());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -39,26 +39,39 @@ import java.util.List;
|
|||||||
public class SwaggerConfig {
|
public class SwaggerConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Docket userApiDocket(ApiInfo apiInfo) {
|
public Docket messageApiDocket() {
|
||||||
return new Docket(DocumentationType.OAS_30)
|
return new Docket(DocumentationType.OAS_30)
|
||||||
.apiInfo(apiInfo)
|
.apiInfo(new ApiInfoBuilder()
|
||||||
.groupName("客户端接口")
|
.title("CIM Push Service APIs.")
|
||||||
|
.description("消息发送相关接口")
|
||||||
|
.version("3.0")
|
||||||
|
.build())
|
||||||
|
.groupName("1、消息相关接口")
|
||||||
.select()
|
.select()
|
||||||
.apis(RequestHandlerSelectors.basePackage("com.farsunset.cim.mvc.controller.api"))
|
.apis(RequestHandlerSelectors.basePackage("com.farsunset.cim.mvc.controller.message"))
|
||||||
.build()
|
.build()
|
||||||
.securitySchemes(securitySchemes())
|
.securitySchemes(securitySchemes())
|
||||||
.securityContexts(securityContexts());
|
.securityContexts(securityContexts());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ApiInfo apiInfo() {
|
public Docket webrtcApiDocket() {
|
||||||
return new ApiInfoBuilder()
|
return new Docket(DocumentationType.OAS_30)
|
||||||
|
.apiInfo(new ApiInfoBuilder()
|
||||||
.title("CIM Push Service APIs.")
|
.title("CIM Push Service APIs.")
|
||||||
.description("CIM客户端接口文档")
|
.description("可用于webrtc通话信令接口")
|
||||||
.version("3.0")
|
.version("3.0")
|
||||||
.build();
|
.build())
|
||||||
|
.groupName("2、Webrtc相关接口")
|
||||||
|
.select()
|
||||||
|
.apis(RequestHandlerSelectors.basePackage("com.farsunset.cim.mvc.controller.webrtc"))
|
||||||
|
.build()
|
||||||
|
.securitySchemes(securitySchemes())
|
||||||
|
.securityContexts(securityContexts());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private List<SecurityScheme> securitySchemes() {
|
private List<SecurityScheme> securitySchemes() {
|
||||||
List<SecurityScheme> schemeList = new ArrayList<>();
|
List<SecurityScheme> schemeList = new ArrayList<>();
|
||||||
schemeList.add(new ApiKey("access-token", "access-token", "header"));
|
schemeList.add(new ApiKey("access-token", "access-token", "header"));
|
||||||
|
@ -21,11 +21,13 @@
|
|||||||
*/
|
*/
|
||||||
package com.farsunset.cim.mvc.controller.admin;
|
package com.farsunset.cim.mvc.controller.admin;
|
||||||
|
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@Api(produces = "application/json", tags = "页面导航",hidden = true)
|
||||||
public class NavigationController {
|
public class NavigationController {
|
||||||
|
|
||||||
@GetMapping(value = "/")
|
@GetMapping(value = "/")
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
package com.farsunset.cim.mvc.controller.admin;
|
package com.farsunset.cim.mvc.controller.admin;
|
||||||
|
|
||||||
import com.farsunset.cim.service.SessionService;
|
import com.farsunset.cim.service.SessionService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@ -30,6 +31,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@Api(produces = "application/json", tags = "在线用户页面",hidden = true)
|
||||||
@RequestMapping("/console/session")
|
@RequestMapping("/console/session")
|
||||||
public class SessionController {
|
public class SessionController {
|
||||||
|
|
||||||
|
@ -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 com.farsunset.cim.service.SessionService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
@ -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.component.push.DefaultMessagePusher;
|
||||||
import com.farsunset.cim.model.Message;
|
import com.farsunset.cim.model.Message;
|
@ -19,7 +19,7 @@
|
|||||||
* *
|
* *
|
||||||
***************************************************************************************
|
***************************************************************************************
|
||||||
*/
|
*/
|
||||||
package com.farsunset.cim.mvc.controller.api;
|
package com.farsunset.cim.mvc.controller.webrtc;
|
||||||
|
|
||||||
|
|
||||||
import com.farsunset.cim.annotation.AccessToken;
|
import com.farsunset.cim.annotation.AccessToken;
|
@ -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.annotation.UID;
|
||||||
import com.farsunset.cim.component.push.DefaultMessagePusher;
|
import com.farsunset.cim.component.push.DefaultMessagePusher;
|
@ -45,7 +45,7 @@ public class TokenInterceptor implements HandlerInterceptor {
|
|||||||
private AccessTokenService accessTokenService;
|
private AccessTokenService accessTokenService;
|
||||||
|
|
||||||
@Override
|
@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);
|
String token = request.getHeader(HEADER_TOKEN);
|
||||||
|
|
||||||
|
@ -83,6 +83,10 @@ public class ReplyBody implements Serializable, Transportable {
|
|||||||
data.put(k, v);
|
data.put(k, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void put(String k, Number v) {
|
||||||
|
data.put(k, v.toString());
|
||||||
|
}
|
||||||
|
|
||||||
public String get(String k) {
|
public String get(String k) {
|
||||||
return data.get(k);
|
return data.get(k);
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,22 @@ public class SentBody implements Serializable {
|
|||||||
return data.getOrDefault(key,defaultValue);
|
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() {
|
public long getTimestamp() {
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user