Browse Source

#集中监控websocket修改

zhongwei
yang_shj 2 years ago
parent
commit
51bec2b141
  1. 6
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/config/WebSocketConfig.java
  2. 24
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MessageHandler.java
  3. 16
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/MyInterceptor.java
  4. 3
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SessionManager.java
  5. 5
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/SocketPool.java
  6. 13
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/WebsocketRun.java

6
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.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter; import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @author ysj
*/
@Configuration @Configuration
@EnableWebSocket @EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer{ public class WebSocketConfig implements WebSocketConfigurer{
@Override @Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/comprehensiveD/{uid}") registry.addHandler(myHandler(), "/comprehensiveD/{uid}").setAllowedOrigins("*");
.setAllowedOrigins("*");
} }
@Bean @Bean

24
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.alibaba.fastjson.JSONObject;
import com.hnac.hzims.operational.config.vo.MessageParamVo; import com.hnac.hzims.operational.config.vo.MessageParamVo;
import com.hnac.hzims.operational.station.service.IRealMonitorService; import com.hnac.hzims.operational.station.service.IRealMonitorService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.ObjectUtil; import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil; 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 org.springframework.web.socket.handler.TextWebSocketHandler;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author ysj
*/
@Slf4j @Slf4j
public class MessageHandler extends TextWebSocketHandler { public class MessageHandler extends TextWebSocketHandler {
private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); private final Long defaultRealDataRefreshTime = 20000L;
private Long defaultRealDataRefreshTime = 20000L;
@Autowired @Autowired
private IRealMonitorService monitorService; private IRealMonitorService monitorService;
private WebSocketSession session;
@Override @Override
public void afterConnectionEstablished(WebSocketSession session) { public void afterConnectionEstablished(WebSocketSession session) {
this.session = session;
String[] split = session.getUri().toString().split("/"); String[] split = session.getUri().toString().split("/");
String uid = split[split.length - 1]; String uid = split[split.length - 1];
SessionManager.add(uid, session); SessionManager.add(uid, session);
@ -45,20 +39,16 @@ public class MessageHandler extends TextWebSocketHandler {
@Override @Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
SessionManager.removeAndClose(session.getId()); 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("sessionId: " + session.getId());
log.info("uri: " + session.getUri()); log.info("uri: " + session.getUri());
log.info("session closed successful!"); log.info("session closed successful!");
} }
@Override @Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { public void handleTransportError(WebSocketSession session, Throwable exception) {
SessionManager.removeAndClose(session.getId()); SessionManager.removeAndClose(session.getId());
if (SocketPool.pool.containsKey(session.getId())) { SocketPool.pool.remove(session.getId());
SocketPool.pool.remove(session.getId());
}
} }
@Override @Override

16
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.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor; import org.springframework.web.socket.server.HandshakeInterceptor;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* @author ysj
*/
@Component @Component
public class MyInterceptor implements HandshakeInterceptor { public class MyInterceptor implements HandshakeInterceptor {
/** /**
* 握手前 * 握手前
*
* @param request * @param request
* @param response * @param response
* @param wsHandler * @param wsHandler
* @param attributes * @param attributes
* @return * @return
* @throws Exception
*/ */
@Override @Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
System.out.println("握手开始");
// 获得请求参数 // 获得请求参数
HashMap<String, String> paramMap = (HashMap<String, String>) HttpUtil.decodeParamMap(request.getURI().getQuery(), StandardCharsets.UTF_8);
HashMap<String, String> paramMap = (HashMap<String, String>) HttpUtil.decodeParamMap(request.getURI().getQuery(), Charset.forName("utf-8"));
String uid = paramMap.get("token"); String uid = paramMap.get("token");
if (StrUtil.isNotBlank(uid)) { if (StrUtil.isNotBlank(uid)) {
// 放入属性域 // 放入属性域
attributes.put("token", uid); attributes.put("token", uid);
System.out.println("用户 token " + uid + " 握手成功!");
return true; return true;
} }
System.out.println("用户登录已失效");
return false; return false;
} }
@ -52,7 +49,6 @@ public class MyInterceptor implements HandshakeInterceptor {
*/ */
@Override @Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
System.out.println("握手完成");
} }
} }

3
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; import java.util.concurrent.ConcurrentHashMap;
/**
* @author ysj
*/
@Slf4j @Slf4j
public class SessionManager { public class SessionManager {
/** /**

5
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.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/**
* @author ysj
*/
public class SocketPool { public class SocketPool {
public static Map<String, MessageHandler> pool = new ConcurrentHashMap<String, MessageHandler>(); public static Map<String, MessageHandler> pool = new ConcurrentHashMap<>();
} }

13
hzims-service/operational/src/main/java/com/hnac/hzims/operational/config/ws/WebsocketRun.java

@ -1,13 +0,0 @@
package com.hnac.hzims.operational.config.ws;
public class WebsocketRun extends Thread{
@Override
public void run() {
super.run();
}
}
Loading…
Cancel
Save