webusocket修改
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 19m27s
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 19m27s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.light.delivery.config;
|
||||
|
||||
import com.light.delivery.service.LocationWebSocketHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
@@ -10,10 +11,13 @@ import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry
|
||||
@EnableWebSocket
|
||||
public class WebsocketConfig implements WebSocketConfigurer {
|
||||
|
||||
@Autowired
|
||||
private LocationWebSocketHandler locationWebSocketHandler;
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
// 注册位置同步处理器
|
||||
registry.addHandler(new LocationWebSocketHandler(), "/ws/location")
|
||||
registry.addHandler(locationWebSocketHandler, "/ws/location")
|
||||
.setAllowedOrigins("*");
|
||||
}
|
||||
}
|
@@ -81,7 +81,12 @@ public class LocationWebSocketHandler extends TextWebSocketHandler {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("处理WebSocket消息时出错: " + e.getMessage());
|
||||
// 确保不因异常导致连接关闭
|
||||
try {
|
||||
session.sendMessage(new TextMessage("{\"error\":\"消息处理失败\"}"));
|
||||
} catch (IOException ioException) {
|
||||
System.err.println("发送错误消息时出错: " + ioException.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,8 +121,8 @@ public class LocationWebSocketHandler extends TextWebSocketHandler {
|
||||
if (deliveryPersonId != null) {
|
||||
sessions.put(deliveryPersonId, session);
|
||||
// 默认状态为未签到
|
||||
deliveryPersonStatus.put(deliveryPersonId, false);
|
||||
lastUpdateTimes.put(deliveryPersonId, System.currentTimeMillis());
|
||||
deliveryPersonStatus.putIfAbsent(deliveryPersonId, false);
|
||||
lastUpdateTimes.putIfAbsent(deliveryPersonId, System.currentTimeMillis());
|
||||
session.sendMessage(new TextMessage("{\"type\":\"subscribed\",\"deliveryPersonId\":" + deliveryPersonId + "}"));
|
||||
}
|
||||
}
|
||||
@@ -171,8 +176,16 @@ public class LocationWebSocketHandler extends TextWebSocketHandler {
|
||||
WebSocketSession session = entry.getValue();
|
||||
|
||||
// 只发送给已签到的配送员
|
||||
if (session.isOpen() && Boolean.TRUE.equals(deliveryPersonStatus.get(deliveryPersonId))) {
|
||||
if (session != null && session.isOpen() && Boolean.TRUE.equals(deliveryPersonStatus.get(deliveryPersonId))) {
|
||||
try {
|
||||
session.sendMessage(textMessage);
|
||||
} catch (IOException e) {
|
||||
System.err.println("向配送员 " + deliveryPersonId + " 发送消息时出错: " + e.getMessage());
|
||||
// 移除已断开的连接
|
||||
sessions.remove(deliveryPersonId);
|
||||
deliveryPersonStatus.remove(deliveryPersonId);
|
||||
lastUpdateTimes.remove(deliveryPersonId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -198,6 +211,7 @@ public class LocationWebSocketHandler extends TextWebSocketHandler {
|
||||
deliveryPersonStatus.remove(deliveryPersonId);
|
||||
lastUpdateTimes.remove(deliveryPersonId);
|
||||
}
|
||||
// 不要关闭session,因为可能已经关闭了
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user