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. 20
      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.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

20
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,21 +39,17 @@ 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());
}
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());
}
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage textMessage) throws IOException {

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.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<String, Object> attributes) throws Exception {
System.out.println("握手开始");
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
// 获得请求参数
HashMap<String, String> paramMap = (HashMap<String, String>) HttpUtil.decodeParamMap(request.getURI().getQuery(), Charset.forName("utf-8"));
HashMap<String, String> paramMap = (HashMap<String, String>) 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("握手完成");
}
}

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;
/**
* @author ysj
*/
@Slf4j
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.concurrent.ConcurrentHashMap;
/**
* @author ysj
*/
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