提交websocket相关代码

dev_2.0.0
liu 1 year ago
parent 2ce1252556
commit 5736487568

@ -5,11 +5,13 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
@SpringBootApplication
@MapperScan(basePackages = {"com.supervision.**.mapper"})
@EnableWebSocket
@EnableScheduling
public class VirtualPatientApplication {
public static void main(String[] args) {

@ -59,14 +59,17 @@ public class UserResourceCheck {
*/
@Scheduled(fixedDelay = 5 * 1000L)
public void keepalive() {
log.info("websocket保活接口开始,每5秒钟发送一次消息");
for (Map.Entry<String, Session> entries : WebSocketServer.SESSION_POOL.entrySet()) {
String userId = entries.getKey();
Session session = entries.getValue();
if (ObjectUtil.isNotEmpty(session)) {
try {
session.getBasicRemote().sendText(JSONUtil.toJsonStr(new Keepalive()));
log.info("用户:{}的websocket保活成功", userId);
} catch (IOException e) {
log.error("用户:{}的websocket连接异常", e.getMessage());
log.error("用户:{}的websocket连接异常", userId, e);
// 连接异常的用户,移除
WebSocketServer.SESSION_POOL.remove(userId);
// 移除redis中该用户的缓存
@ -74,7 +77,7 @@ public class UserResourceCheck {
}
}
}
log.info("websocket保活接口结束");
}
// 实现一个方法用于踢下线用户,走的是Redis的消息队列

Loading…
Cancel
Save