diff --git a/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/config/WebSocketConfig.java b/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/config/WebSocketConfig.java new file mode 100644 index 00000000..f8c24e06 --- /dev/null +++ b/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/config/WebSocketConfig.java @@ -0,0 +1,42 @@ +package com.ruoyi.cai.ws.config; + +import cn.hutool.core.util.StrUtil; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.server.HandshakeInterceptor; + +/** + * WebSocket 配置 + * + * @author zendwang + */ +@Configuration +@ConditionalOnProperty(value = "websocket.enabled", havingValue = "true") +@EnableConfigurationProperties(WebSocketProperties.class) +@EnableWebSocket +public class WebSocketConfig { + + @Bean + public WebSocketConfigurer webSocketConfigurer(HandshakeInterceptor handshakeInterceptor, + WebSocketHandler webSocketHandler, + WebSocketProperties webSocketProperties) { + if (StrUtil.isBlank(webSocketProperties.getPath())) { + webSocketProperties.setPath("/ws"); + } + + if (StrUtil.isBlank(webSocketProperties.getAllowedOrigins())) { + webSocketProperties.setAllowedOrigins("*"); + } + + return registry -> registry + .addHandler(webSocketHandler, webSocketProperties.getPath()) + .addInterceptors(handshakeInterceptor) + .setAllowedOrigins(webSocketProperties.getAllowedOrigins()); + } + +} diff --git a/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/dto/WsRMsgGen.java b/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/dto/WsRMsgGen.java index 65d739fd..a6c9d227 100644 --- a/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/dto/WsRMsgGen.java +++ b/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/dto/WsRMsgGen.java @@ -8,6 +8,16 @@ import java.util.Map; public class WsRMsgGen { + public static WsR request(Long roomId){ + Map map = new HashMap<>(); + map.put("roomid",roomId); + WsR> ok = WsR.ok(map); + ok.setMethod("request"); + ok.setMsg("连线成功"); + return ok; + } + + public static WsR response(Long roomId){ Map map = new HashMap<>(); map.put("roomid",roomId); diff --git a/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/handler/RoomWebSocketHandler.java b/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/handler/RoomWebSocketHandler.java index 0f9528f6..d1cb6f00 100644 --- a/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/handler/RoomWebSocketHandler.java +++ b/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/handler/RoomWebSocketHandler.java @@ -1,27 +1,16 @@ package com.ruoyi.cai.ws.handler; -import cn.hutool.core.util.URLUtil; -import com.google.common.base.Splitter; -import com.ruoyi.cai.chat.ChatManager; -import com.ruoyi.cai.ws.cache.FdCtxDataCache; -import com.ruoyi.cai.ws.constant.WebSocketConstants; import com.ruoyi.cai.ws.holder.WebSocketSessionHolder; import com.ruoyi.cai.ws.processon.OpenLogic; -import com.ruoyi.cai.ws.service.RoomService; import com.ruoyi.cai.ws.util.WebSocketUtils; import com.ruoyi.common.utils.StringUtils; import lombok.extern.slf4j.Slf4j; -import org.apache.http.client.utils.URIUtils; -import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.socket.*; import org.springframework.web.socket.handler.AbstractWebSocketHandler; -import javax.websocket.server.PathParam; -import java.io.IOException; import java.net.URI; -import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -109,10 +98,6 @@ public class RoomWebSocketHandler extends AbstractWebSocketHandler { log.error("[transport error] sessionId: {} , exception:{}", session.getId(), exception.getMessage()); } - @Autowired - private RoomService roomService; - @Autowired - private FdCtxDataCache fdCtxDataCache; /** * 连接关闭后 * diff --git a/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/processon/OpenLogic.java b/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/processon/OpenLogic.java index 0514d217..38190579 100644 --- a/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/processon/OpenLogic.java +++ b/ruoyi-cai/src/main/java/com/ruoyi/cai/ws/processon/OpenLogic.java @@ -147,7 +147,7 @@ public class OpenLogic { RoomStatusEnums.STATUS_CALLER_CONNECT.getCode().equals(status) || RoomStatusEnums.STATUS_RECEIVER_CONNECT.getCode().equals(status)){ // 给当前会话发送消息 - 连线成功 - RoomWebSocketUtil.sendSendMessage(session, WsRMsgGen.response(room.getRoomId())); + RoomWebSocketUtil.sendSendMessage(session, WsRMsgGen.request(room.getRoomId())); } if(isFirst){ yunxinWsService.sendCallAsync(room.getRoomId(),room.getCallId(),room.getReceiverId());