|
|
|
@ -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的消息队列
|
|
|
|
|