Monday 8 March 2021

Check if a WebSocket server is available without erroring

My web app needs to periodically check if the server is up. This allows users to start the server and client in either order.

To do this, I'm simply calling the WebSocket constructor from inside a setTimeout handler.

This strategy works well from a usage standpoint, but it is annoying for me the developer, because I see a lot of console errors in Chrome:

remote.html:194 WebSocket connection to 'ws://localhost:8082/' failed: Connection closed before receiving a handshake response
remote.html:194 WebSocket connection to 'ws://localhost:8082/' failed: Connection closed before receiving a handshake response
remote.html:194 WebSocket connection to 'ws://localhost:8082/' failed: Connection closed before receiving a handshake response

These are not really errors because my web app is perfectly fine with the server being unavailable. It is repeatedly calling the WebSocket constructor (MDN) merely to check if the server has been started yet.

Is there a better way to do this? For context, my web app is a "remote control" for a native C++ app that runs on a device.



from Check if a WebSocket server is available without erroring

No comments:

Post a Comment