diff --git a/src/main/java/com/light/delivery/service/LocationWebSocketHandler.java b/src/main/java/com/light/delivery/service/LocationWebSocketHandler.java index 51b9811..928c0d1 100644 --- a/src/main/java/com/light/delivery/service/LocationWebSocketHandler.java +++ b/src/main/java/com/light/delivery/service/LocationWebSocketHandler.java @@ -132,11 +132,9 @@ public class LocationWebSocketHandler extends TextWebSocketHandler { */ public void userSignedOut(Long userId) { if (userId != null) { - // 从所有映射中移除用户信息 - sessions.remove(userId); - userFullInfos.remove(userId); - userLocations.remove(userId); - + // 执行清理操作 + cleanupUserData(userId); + System.out.println("用户 " + userId + " 已签退"); // 广播更新后的在线用户列表 @@ -605,11 +603,19 @@ public class LocationWebSocketHandler extends TextWebSocketHandler { */ public void cleanupUserConnection(Long userId) { if (userId != null) { - sessions.remove(userId); - // 注意:这里不清理userFullInfos和userLocations,因为用户可能只是断开了WebSocket连接但未签退 - // 签退应该通过明确的签退操作来处理 + cleanupUserData(userId); } } + + /** + * 清理用户相关数据的通用方法 + * @param userId 用户ID + */ + private void cleanupUserData(Long userId) { + sessions.remove(userId); + userFullInfos.remove(userId); + userLocations.remove(userId); + } /** * 定时检查并清理过期的连接和位置信息(每30秒执行一次)