Jayson Bailey
Jayson Bailey

Reputation: 1004

How can I send a disconnect message to websocket before leaving page

So basically, if I have say a chat application using websockets, I want to be able to send the server a message saying I'm disconnecting. I've tried binding to onbeforeunload, but that seems to only be able to display a message.

Is there a better strategy to this? If not, it seems like I'd have to do some sort of ping/timeout strategy from the server side.

Thanks.

Upvotes: 1

Views: 2203

Answers (1)

moka
moka

Reputation: 23045

WebSockets are implemented on top of TCP, and in TCP world different connection drops - is expected scenario.

In terms of network security - your server side should never rely on "kindness" of clients, and should always be independent and expect any and more scenario of disconnects.

Taking this in account, you have to expect unexpectedly dropped connections (GPRS/EDGE/3G/4G poor connection or going under bridge/ground, wifi drops, ethernet unplug, ISP issues, server network problems, and many more). Dropped connections without "bye" in fact is most common disconnects you will get, so have to implement server side logic taking this in account.

Upvotes: 2

Related Questions