TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.53k stars 2.58k forks source link

How to trap GOING_AWAY #1398

Closed lanner17 closed 8 months ago

lanner17 commented 9 months ago

Hi In my websocket server application, clients navigate away from the server all the time. I assume the "event" GOING_AWAYis generated when this happens. I would like to release resources in the server when this happens. However, it does to seem that GOING_AWAYfires the onClose()message in the server. Is there another way to trap the GOING_AWAYevent ? (It is version 1.4.1)

Thank you

PhilipRoman commented 9 months ago

Hi, first of all 1.4.1 is an old version and I would strongly recommend to update it (please read https://github.com/TooTallNate/Java-WebSocket/security/advisories/GHSA-gw55-jm4h-x339)

I'm not sure what you mean here, do you want the GOING_AWAY event to not close the socket? Otherwise, why not free resources in onClose?

lanner17 commented 9 months ago

@PhilipRoman Thanks for the recommendation to upgrade - made a note to upgrade.

Indeed, I want to close the socket when there is a GOING_AWAY event. I expected that onClose() will be called when the client navigates away so that I can free resources when onClose() is called. However, onClose() is not being called when GOING_AWAY occurs - at least as far as I can tell (I am using v1.5.6).

Basically, I would like to find a way to detect when a client goes away, to free up resources that it created.

PhilipRoman commented 9 months ago

Cannot reproduce, for me connection is closed correctly when I navigate away from a HTML page (1001) or close the tab (1006). You can confirm with Wireshark that a close packet is indeed sent. It should look like this: image If you see this packet, but no close event is generated, then it is indeed a bug.

Alternatively, you can set the connection lost timeout to something lower than default (https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection) and then any socket will be cleaned up in reasonable time, even if it doesn't disconnect properly.

lanner17 commented 8 months ago

@PhilipRoman Apologies for the delay. I agree, browsers do close the web socket if you navigate away or close the tab/page. The case I was thinking about is when a program has a handle to a web socket and then open another one with the same handle without closing the previous one. I assumed the first one will time out after a while. However, using a tool to look at the TCP statuses, one can see that the connection stays established, hence it will not time out. However, both sockets will close when one navigates away or close the page. It was a wrong assumption on my end. Thank you for the response