当多个连接打开时SockJS连接将被关闭

发布于 2025-01-14 10:13:22 字数 817 浏览 0 评论 0原文

如果仅打开一个 SockJS(轮询 XHR)连接,则该应用程序可以正常工作。一旦我在新窗口中打开它,连接就会定期关闭。客户端是SockJS客户端,后端是启用了MVC和SockJS的Spring Boot。

这是我在服务器端看到的:

2022-03-14 10:40:20.992 DEBUG 752 --- [nio-8311-exec-7] a.w.b.u.d.websocket.WebSocketHandler     : connection opened, id: gqgmlrff
2022-03-14 10:40:21.015 DEBUG 752 --- [nio-8311-exec-9] a.w.b.u.d.websocket.WebSocketHandler     : Server received message: {"action":"subscribe","payload":{"id":"c910f5d1-9e16-4e30-9559-e0e27973177b","entityType":"PROJEKT"}}

15-20 秒后

2022-03-14 10:40:40.075 DEBUG 752 --- [      SockJS-10] a.w.b.u.d.websocket.WebSocketHandler     : connection closed, sessionId: gqgmlrff, status: CloseStatus[code=3000, reason=Go away!]

这会在两个窗口的会话中无限重复。 会话关闭似乎将由后端启动,因为在客户端将执行 .onclose() 处理程序。

If only one SockJS (polling XHR) connection is open, then the app works fine. As soon as I additionally open it in a new window, then the connections will periodically be closed. The client is SockJS client, the backend is Spring Boot with MVC and SockJS enabled.

This is what I see on the server side:

2022-03-14 10:40:20.992 DEBUG 752 --- [nio-8311-exec-7] a.w.b.u.d.websocket.WebSocketHandler     : connection opened, id: gqgmlrff
2022-03-14 10:40:21.015 DEBUG 752 --- [nio-8311-exec-9] a.w.b.u.d.websocket.WebSocketHandler     : Server received message: {"action":"subscribe","payload":{"id":"c910f5d1-9e16-4e30-9559-e0e27973177b","entityType":"PROJEKT"}}

after 15-20 seconds

2022-03-14 10:40:40.075 DEBUG 752 --- [      SockJS-10] a.w.b.u.d.websocket.WebSocketHandler     : connection closed, sessionId: gqgmlrff, status: CloseStatus[code=3000, reason=Go away!]

This repeats infinitely with sessions of both windows.
It seems that the session closing will be initiated by the backend, since on client side the .onclose() handler will be executed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里°也失望 2025-01-21 10:13:22

原因是,对于两个应用程序实例,都生成了不同的身份验证令牌,并且相等性未更新。如下更新 equals() 和 hashCode() 后,请求已正确处理。

    override fun equals(obj: Any?): Boolean = obj is KeycloakToken &&
            principal == obj.principal &&
            authorities == obj.authorities &&
            isAuthenticated == obj.isAuthenticated


    override fun hashCode(): Int {
        var code = 31
        for (authority in authorities) {
            code = code xor authority.hashCode()
        }
        code = code xor principal.hashCode()
        if (this.isAuthenticated) {
            code = code xor -37
        }
        return code
    }

The reason was that for both app instances different auth tokens have been generated and the equality wasn't updated. After updating equals() and hashCode() as follow the requests were correctly processed.

    override fun equals(obj: Any?): Boolean = obj is KeycloakToken &&
            principal == obj.principal &&
            authorities == obj.authorities &&
            isAuthenticated == obj.isAuthenticated


    override fun hashCode(): Int {
        var code = 31
        for (authority in authorities) {
            code = code xor authority.hashCode()
        }
        code = code xor principal.hashCode()
        if (this.isAuthenticated) {
            code = code xor -37
        }
        return code
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文