This commit is contained in:
张良(004796)
2024-02-26 11:30:53 +08:00
parent 0ccfeeb353
commit 0ae74a2a02
4 changed files with 53 additions and 16 deletions

View File

@@ -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());
}
}

View File

@@ -8,6 +8,16 @@ import java.util.Map;
public class WsRMsgGen { public class WsRMsgGen {
public static WsR request(Long roomId){
Map<String,Object> map = new HashMap<>();
map.put("roomid",roomId);
WsR<Map<String, Object>> ok = WsR.ok(map);
ok.setMethod("request");
ok.setMsg("连线成功");
return ok;
}
public static WsR response(Long roomId){ public static WsR response(Long roomId){
Map<String,Object> map = new HashMap<>(); Map<String,Object> map = new HashMap<>();
map.put("roomid",roomId); map.put("roomid",roomId);

View File

@@ -1,27 +1,16 @@
package com.ruoyi.cai.ws.handler; 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.holder.WebSocketSessionHolder;
import com.ruoyi.cai.ws.processon.OpenLogic; import com.ruoyi.cai.ws.processon.OpenLogic;
import com.ruoyi.cai.ws.service.RoomService;
import com.ruoyi.cai.ws.util.WebSocketUtils; import com.ruoyi.cai.ws.util.WebSocketUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.socket.*; import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.AbstractWebSocketHandler; import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -109,10 +98,6 @@ public class RoomWebSocketHandler extends AbstractWebSocketHandler {
log.error("[transport error] sessionId: {} , exception:{}", session.getId(), exception.getMessage()); log.error("[transport error] sessionId: {} , exception:{}", session.getId(), exception.getMessage());
} }
@Autowired
private RoomService roomService;
@Autowired
private FdCtxDataCache fdCtxDataCache;
/** /**
* 连接关闭后 * 连接关闭后
* *

View File

@@ -147,7 +147,7 @@ public class OpenLogic {
RoomStatusEnums.STATUS_CALLER_CONNECT.getCode().equals(status) || RoomStatusEnums.STATUS_CALLER_CONNECT.getCode().equals(status) ||
RoomStatusEnums.STATUS_RECEIVER_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){ if(isFirst){
yunxinWsService.sendCallAsync(room.getRoomId(),room.getCallId(),room.getReceiverId()); yunxinWsService.sendCallAsync(room.getRoomId(),room.getCallId(),room.getReceiverId());