diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/component/handler/MessageHandler.java b/cim-boot-server/src/main/java/com/farsunset/cim/component/handler/MessageHandler.java new file mode 100644 index 0000000..bf4c3c2 --- /dev/null +++ b/cim-boot-server/src/main/java/com/farsunset/cim/component/handler/MessageHandler.java @@ -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()); + + } +} diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/config/SwaggerConfig.java b/cim-boot-server/src/main/java/com/farsunset/cim/config/SwaggerConfig.java index 405f917..290b0ac 100644 --- a/cim-boot-server/src/main/java/com/farsunset/cim/config/SwaggerConfig.java +++ b/cim-boot-server/src/main/java/com/farsunset/cim/config/SwaggerConfig.java @@ -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 securitySchemes() { List schemeList = new ArrayList<>(); schemeList.add(new ApiKey("access-token", "access-token", "header")); diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/admin/NavigationController.java b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/admin/NavigationController.java index b776617..4fa1d35 100644 --- a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/admin/NavigationController.java +++ b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/admin/NavigationController.java @@ -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 = "/") diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/admin/SessionController.java b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/admin/SessionController.java index 780e919..8588dec 100644 --- a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/admin/SessionController.java +++ b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/admin/SessionController.java @@ -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 { diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/APNsController.java b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/message/APNsController.java similarity index 98% rename from cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/APNsController.java rename to cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/message/APNsController.java index cf4cc18..6075818 100644 --- a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/APNsController.java +++ b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/message/APNsController.java @@ -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; diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/MessageController.java b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/message/MessageController.java similarity index 98% rename from cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/MessageController.java rename to cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/message/MessageController.java index 6c9a0ea..df5058e 100644 --- a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/MessageController.java +++ b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/message/MessageController.java @@ -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; diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/UserController.java b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/webrtc/UserController.java similarity index 98% rename from cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/UserController.java rename to cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/webrtc/UserController.java index f9f7807..10c5798 100644 --- a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/UserController.java +++ b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/webrtc/UserController.java @@ -19,7 +19,7 @@ * * *************************************************************************************** */ -package com.farsunset.cim.mvc.controller.api; +package com.farsunset.cim.mvc.controller.webrtc; import com.farsunset.cim.annotation.AccessToken; diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/WebrtcController.java b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/webrtc/WebrtcController.java similarity index 99% rename from cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/WebrtcController.java rename to cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/webrtc/WebrtcController.java index de63e34..31d1148 100644 --- a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/api/WebrtcController.java +++ b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/controller/webrtc/WebrtcController.java @@ -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; diff --git a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/interceptor/TokenInterceptor.java b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/interceptor/TokenInterceptor.java index 351027a..a2034a1 100644 --- a/cim-boot-server/src/main/java/com/farsunset/cim/mvc/interceptor/TokenInterceptor.java +++ b/cim-boot-server/src/main/java/com/farsunset/cim/mvc/interceptor/TokenInterceptor.java @@ -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); diff --git a/cim-server-sdk/src/main/java/com/farsunset/cim/model/ReplyBody.java b/cim-server-sdk/src/main/java/com/farsunset/cim/model/ReplyBody.java index 350c3a5..bb7deec 100644 --- a/cim-server-sdk/src/main/java/com/farsunset/cim/model/ReplyBody.java +++ b/cim-server-sdk/src/main/java/com/farsunset/cim/model/ReplyBody.java @@ -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); } diff --git a/cim-server-sdk/src/main/java/com/farsunset/cim/model/SentBody.java b/cim-server-sdk/src/main/java/com/farsunset/cim/model/SentBody.java index 357112a..aefbf5d 100644 --- a/cim-server-sdk/src/main/java/com/farsunset/cim/model/SentBody.java +++ b/cim-server-sdk/src/main/java/com/farsunset/cim/model/SentBody.java @@ -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; }