【发布时间】:2019-10-29 11:39:53
【问题描述】:
我在使用 Web 套接字的 Spring Boot 应用程序中有一个严重的错误。随机发生在我的MyWebSocketHandler 类重写方法handleTextMessage 或handleBinaryMessage 方法在afterConnectionClosed 之后调用。
我认为这是一个客户的问题,但多个客户导致了同样的问题。
这是我拥有的简化代码。
@Component
public class MyWebSocketHandler extends AbstractWebSocketHandler {
private Map<String, ClientSession> activeSessions = new ConcurrentHashMap<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
activeSessions.put(session.getId(), clientSession);
// doing work
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
activeSessions.remove(session.getId())
// doing work
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
checkSession(session); // Random Exception here
// doing work
}
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
checkSession(session); // Random Exception here
// doing work
}
private void checkSession(WebSocketSession session){
if(activeSessions.get(session.getId() == null){
throw new RuntimeException();
}
}
}
afterConnectionClosed 显示关闭状态 1000 并在同一毫秒内具有相同 sessionId 的新请求到达 handleTextMessage 或 handleBinaryMessage 并引发异常,因为该记录已从 activeSessions 映射中删除。
关于这种行为的可能原因有什么想法吗?
【问题讨论】:
标签: java websocket spring-websocket