当前位置: 首页 > 工具软件 > PingPong > 使用案例 >

springbot websocket ping pong消息心跳检测

陆烨烁
2023-12-01

@OnMessage

public void onPong(PongMessage pongMessage,Session session) {

log.info("接收pong消息"+userId);

ByteBuffer byteBuffer = pongMessage.getApplicationData();

this.state=1;

}

@Scheduled(cron = "0 0/2 * * * ? ")

public void heartbeat() throws IOException {

for (WebSocketServer item : webSocketSet) {

try {

if (this.state!=1){

log.error("上轮检测未通过,移除"+userId);

webSocketSet.remove(item);

subOnlineCount();

}else {

item.session.getBasicRemote().sendPing(new PingMessage().getPayload());

item.state = 2;

}

} catch (IOException e) {

log.error("错误,移除websocket UserId:"+item.userId);

webSocketSet.remove(item);

subOnlineCount();

}

}

}

 类似资料: