feat: 增加websocket通知

dev
huangfeng 1 year ago
parent d29cfbf3ee
commit 201279abea

@ -60,6 +60,7 @@ public class SecurityConfig {
urlWhiteList.add("/xymanager/swagger-ui.html");
urlWhiteList.add("/xymanager/swagger-resources/**");
urlWhiteList.add("/xymanager/*/api-docs");
urlWhiteList.add("/xymanager/websocket/**");
urlWhiteList.add("/test/**");
urlWhiteList.add("/error");
urlWhiteList.add("/test/**");

@ -40,10 +40,6 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- SpringBoot 拦截器 -->
@ -51,6 +47,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- 阿里数据库连接池 -->
<dependency>

@ -0,0 +1,20 @@
package com.shxy.xymanager_framework.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
/**
* ServerEndpointExporter
*
* Bean使@ServerEndpointwebsocket endpoint
*
* @return ServerEndpointExporter
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}

@ -0,0 +1,63 @@
package com.shxy.xymanager_framework.socket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ServerEndpoint(value = "/websocket")
@Component
@Slf4j
public class WebSocketServer {
private static List<Session> sessionPools = new ArrayList<>();
@OnOpen
public void onOpen(Session session) {
sessionPools.add(session);
log.info("当前socket连接数" + sessionPools.size());
}
@OnClose
public void onClose(Session session) {
sessionPools.remove(session);
log.info("剩余socket连接数" + sessionPools.size());
}
public void sendMessage(String message) {
Iterator<Session> it = sessionPools.listIterator();
while (it.hasNext()) {
Session session = it.next();
try {
session.getBasicRemote().sendText(message);
} catch (IOException ignore) {
}
}
}
//收到客户端信息
@OnMessage
public void onMessage(Session session, String message) {
try {
if (!"1".equals(message)) {
message = message.replaceAll("吗", "")
.replaceAll("你", "我")
.replaceAll("", "!")
.replaceAll("\\?", "!");
session.getBasicRemote().sendText(message);
}
} catch (IOException ignore) {
}
}
//错误时调用
@OnError
public void onError(Session session, Throwable throwable) {
}
}

@ -4,6 +4,7 @@ import com.shxy.xymanager_common.entity.MntnRawReportsExample;
import com.shxy.xymanager_common.entity.Terminals;
import com.shxy.xymanager_common.util.DingTalkPushUtil;
import com.shxy.xymanager_dao.dao.MntnRawReportsMapper;
import com.shxy.xymanager_framework.socket.WebSocketServer;
import com.shxy.xymanager_service.service.CacheService;
import com.shxy.xymanager_service.service.NewCacheService;
import lombok.extern.slf4j.Slf4j;
@ -33,6 +34,8 @@ public class MntnCheckTask {
NewCacheService newCacheService;
@Autowired
private CacheService cacheService;
@Resource
WebSocketServer webSocketServer;
private int shutdown = 0;
private List<String> alertList = new ArrayList<>();
@ -52,6 +55,7 @@ public class MntnCheckTask {
if (shutdown == 0 && alertList.size() > 0) {
String str = alertList.get(0);
alertList.remove(0);
webSocketServer.sendMessage(str);
DingTalkPushUtil.pushText(str);
}
}

Loading…
Cancel
Save