消息推送连接关闭时避免报错
This commit is contained in:
@@ -87,7 +87,19 @@ public class NotificationConnection {
|
|||||||
// 判断用户ID与当前的Key相等
|
// 判断用户ID与当前的Key相等
|
||||||
if (userId.equals(key)) {
|
if (userId.equals(key)) {
|
||||||
log.info("私发消息 【给用户】 :{}发送消息【{}】", key, message); // 打印
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
|
|||||||
data.put(type, count);
|
data.put(type, count);
|
||||||
resp.add(data);
|
resp.add(data);
|
||||||
String jsonString = JSON.toJSONString(resp);
|
String jsonString = JSON.toJSONString(resp);
|
||||||
log.info("消息推送 : {}", jsonString);
|
// log.info("消息推送 : {}", jsonString);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
notificationConnection.sendMsg(jsonString, receiverId);
|
notificationConnection.sendMsg(jsonString, receiverId);
|
||||||
|
|||||||
Reference in New Issue
Block a user