'SockJS connections will be closed when multiple connections open
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.
Solution 1:[1]
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
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andras Hatvani |