diff --git a/virtual-patient-web/src/main/java/com/supervision/VirtualPatientApplication.java b/virtual-patient-web/src/main/java/com/supervision/VirtualPatientApplication.java index 31f4f2c9..adf816c6 100644 --- a/virtual-patient-web/src/main/java/com/supervision/VirtualPatientApplication.java +++ b/virtual-patient-web/src/main/java/com/supervision/VirtualPatientApplication.java @@ -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) { diff --git a/virtual-patient-web/src/main/java/com/supervision/usermanage/UserResourceCheck.java b/virtual-patient-web/src/main/java/com/supervision/usermanage/UserResourceCheck.java index c0d66db6..9b0182fa 100644 --- a/virtual-patient-web/src/main/java/com/supervision/usermanage/UserResourceCheck.java +++ b/virtual-patient-web/src/main/java/com/supervision/usermanage/UserResourceCheck.java @@ -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的消息队列