【问题标题】:`handleMessage` is called after `afterConnectionClosed ` in Spring Boot Web Sockets在 Spring Boot Web Sockets 中的 `afterConnectionClosed` 之后调用`handleMessage`
【发布时间】:2019-10-29 11:39:53
【问题描述】:

我在使用 Web 套接字的 Spring Boot 应用程序中有一个严重的错误。随机发生在我的MyWebSocketHandler 类重写方法handleTextMessagehandleBinaryMessage 方法在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 的新请求到达 handleTextMessagehandleBinaryMessage 并引发异常,因为该记录已从 activeSessions 映射中删除。

关于这种行为的可能原因有什么想法吗?

【问题讨论】:

    标签: java websocket spring-websocket


    【解决方案1】:

    在你的会话中使用这样的东西:

     synchronized (session) {
                        do something with session
                    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-22
      • 2017-02-19
      • 2020-07-02
      • 1970-01-01
      • 2019-06-02
      • 2015-03-09
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多