From 51bec2b14102d144dbc905ebcf7f76849bddd99e Mon Sep 17 00:00:00 2001 From: yang_shj <1069818635@QQ.com> Date: Thu, 13 Apr 2023 16:58:42 +0800 Subject: [PATCH] =?UTF-8?q?#=E9=9B=86=E4=B8=AD=E7=9B=91=E6=8E=A7websocket?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../operational/config/config/WebSocketConfig.java | 6 ++++-- .../operational/config/ws/MessageHandler.java | 24 +++++++--------------- .../hzims/operational/config/ws/MyInterceptor.java | 16 ++++++--------- .../operational/config/ws/SessionManager.java | 3 +++ .../hzims/operational/config/ws/SocketPool.java | 5 ++++- .../hzims/operational/config/ws/WebsocketRun.java | 13 ------------ 6 files changed, 24 insertions(+), 43 deletions(-) delete mode 100644 hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/WebsocketRun.java diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/config/WebSocketConfig.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/config/WebSocketConfig.java index 0fca3b6..2da4d77 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/config/WebSocketConfig.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/config/WebSocketConfig.java @@ -13,13 +13,15 @@ import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; import org.springframework.web.socket.server.standard.ServerEndpointExporter; +/** + * @author ysj + */ @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer{ @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { - registry.addHandler(myHandler(), "/comprehensiveD/{uid}") - .setAllowedOrigins("*"); + registry.addHandler(myHandler(), "/comprehensiveD/{uid}").setAllowedOrigins("*"); } @Bean diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MessageHandler.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MessageHandler.java index ff4bab4..7160f5a 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MessageHandler.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MessageHandler.java @@ -3,7 +3,6 @@ package com.hnac.hzims.operational.config.ws; import com.alibaba.fastjson.JSONObject; import com.hnac.hzims.operational.config.vo.MessageParamVo; import com.hnac.hzims.operational.station.service.IRealMonitorService; -import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.utils.ObjectUtil; import org.springblade.core.tool.utils.StringUtil; @@ -14,25 +13,20 @@ import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; import java.io.IOException; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; +/** + * @author ysj + */ @Slf4j public class MessageHandler extends TextWebSocketHandler { - private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); - - private Long defaultRealDataRefreshTime = 20000L; + private final Long defaultRealDataRefreshTime = 20000L; @Autowired private IRealMonitorService monitorService; - private WebSocketSession session; - @Override public void afterConnectionEstablished(WebSocketSession session) { - this.session = session; String[] split = session.getUri().toString().split("/"); String uid = split[split.length - 1]; SessionManager.add(uid, session); @@ -45,20 +39,16 @@ public class MessageHandler extends TextWebSocketHandler { @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { SessionManager.removeAndClose(session.getId()); - if (SocketPool.pool.containsKey(session.getId())) { - SocketPool.pool.remove(session.getId()); - } + SocketPool.pool.remove(session.getId()); log.info("sessionId: " + session.getId()); log.info("uri: " + session.getUri()); log.info("session closed successful!"); } @Override - public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { + public void handleTransportError(WebSocketSession session, Throwable exception) { SessionManager.removeAndClose(session.getId()); - if (SocketPool.pool.containsKey(session.getId())) { - SocketPool.pool.remove(session.getId()); - } + SocketPool.pool.remove(session.getId()); } @Override diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MyInterceptor.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MyInterceptor.java index d2f755d..5f4c58d 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MyInterceptor.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MyInterceptor.java @@ -8,37 +8,34 @@ import org.springframework.stereotype.Component; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.server.HandshakeInterceptor; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; +/** + * @author ysj + */ @Component public class MyInterceptor implements HandshakeInterceptor { /** * 握手前 - * * @param request * @param response * @param wsHandler * @param attributes * @return - * @throws Exception */ @Override - public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { - System.out.println("握手开始"); + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) { // 获得请求参数 - - HashMap paramMap = (HashMap) HttpUtil.decodeParamMap(request.getURI().getQuery(), Charset.forName("utf-8")); + HashMap paramMap = (HashMap) HttpUtil.decodeParamMap(request.getURI().getQuery(), StandardCharsets.UTF_8); String uid = paramMap.get("token"); if (StrUtil.isNotBlank(uid)) { // 放入属性域 attributes.put("token", uid); - System.out.println("用户 token " + uid + " 握手成功!"); return true; } - System.out.println("用户登录已失效"); return false; } @@ -52,7 +49,6 @@ public class MyInterceptor implements HandshakeInterceptor { */ @Override public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { - System.out.println("握手完成"); } } diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SessionManager.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SessionManager.java index d2e5eed..fbd6176 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SessionManager.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SessionManager.java @@ -7,6 +7,9 @@ import java.io.IOException; import java.util.concurrent.ConcurrentHashMap; +/** + * @author ysj + */ @Slf4j public class SessionManager { /** diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SocketPool.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SocketPool.java index f0d2668..a48c9ae 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SocketPool.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SocketPool.java @@ -3,8 +3,11 @@ package com.hnac.hzims.operational.config.ws; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +/** + * @author ysj + */ public class SocketPool { - public static Map pool = new ConcurrentHashMap(); + public static Map pool = new ConcurrentHashMap<>(); } diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/WebsocketRun.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/WebsocketRun.java deleted file mode 100644 index 15b3a80..0000000 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/WebsocketRun.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.hnac.hzims.operational.config.ws; - - - -public class WebsocketRun extends Thread{ - - - - @Override - public void run() { - super.run(); - } -}