lukeed / sockette

The cutest little WebSocket wrapper! 🧦
MIT License
2.45k stars 81 forks source link

Socket connection code choices #57

Closed samholst closed 4 years ago

samholst commented 4 years ago

I was wondering on ws.onclose, why on those specific code numbers we don’t want to try to reconnect. Codes 1003, 1001, and 1005. Why not have it try to always reconnect if it loses a connection?

lukeed commented 4 years ago

Hey,

So if you search through CloseEvent you'll start to get a little more info.

Both 1000 and 1005 are normal closures. 1005 is technically a lack of status code given, but it's still a normal shutdown signal from the server. (See https://github.com/lukeed/sockette/issues/3)

Also, there's no 1003 in Sockette. The 1e3 evaluates to 1000.

The 1001 is "Going Away" which is sent by the client. This can be when the tab is closed (which won't matter anyway) or when the current DOM isn't being wiped. This was a late addition to Sockette, proved necessary by most SPA/React integrations and within React Native apps.

Really, the client should always clean up properly, but isn't always the case. Either way, it's the client telling the server that it's leaving.

Hope that helps!

samholst commented 4 years ago

Thank you!