消息推送连接关闭时避免报错
This commit is contained in:
@@ -87,7 +87,19 @@ public class NotificationConnection {
|
||||
// 判断用户ID与当前的Key相等
|
||||
if (userId.equals(key)) {
|
||||
log.info("私发消息 【给用户】 :{}发送消息【{}】", key, message); // 打印
|
||||
webSocket.session.getBasicRemote().sendText(message); // 则发送给当前的用户即可
|
||||
if (webSocket.session.isOpen()){
|
||||
// 避免因为网络问题或其他原因导致连接突然关闭而报错
|
||||
try {
|
||||
webSocket.session.getBasicRemote().sendText(message); // 则发送给当前的用户即可
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to send message to session {}: {}", webSocket.session.getId(), e.getMessage());
|
||||
// 这里可以选择移除关闭的 session
|
||||
websockets.remove(entry.getKey());
|
||||
}
|
||||
}else {
|
||||
log.info("连接已关闭,sessionId:{}, userId:{}", webSocket.session.getId(), key);
|
||||
websockets.remove(entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user