athanclark / purescript-websocket-moderate

A different websocket binding for purescript
BSD 3-Clause "New" or "Revised" License
3 stars 3 forks source link

DOMException on socket close #3

Open chekoopa opened 4 years ago

chekoopa commented 4 years ago

Whenever close is called (no matter with or without optional data, on client side or server side, like handshake error), browser throws the async exception from this line. https://github.com/athanclark/purescript-websocket-moderate/blob/1694260626d5c92b8393c1f12c420ef7c202cb2d/src/WebSocket.js#L32

The exception text is:

Uncaught DOMException: Failed to execute 'close' on 'WebSocket': 
The code must be either 1000, or between 3000 and 4999. 0 is neither.

My guess is that this line isn't neccessary, or at least it should check readyState before calling.

Just for the record, I'm using v7.0.2. The browser is Chromuim 83.0.4103.61 (dev), (64-bit), NixOS Linux 20.09.

athanclark commented 4 years ago

Ahh, okay shoot, sorry for not catching this. I think you may be right - socket.close :: Effect Unit is just underlyingly calling socket.close() in JavaScript, without socket.close(code,reason) parameters - although, there is also an event listener attached to the "close" event, that relays the call to socket.close() after filing some of the parsed information.

It might take some playing, but I'll get this fixed hopefully this weekend. Does it happen with any socket you are trying to open? If not, would you be willing to provide a minimal example? Thank you again for catching this.

chekoopa commented 4 years ago

It doesn't matter close or close' was called. I suppose when this close handler is called, the socket is closed already, thus this socket.close(e) may be out of place. Moreover, the handler calls it with the Event object instead of proper (code, reason).

I can't provide you a truly minimal example, but here's my Websocket client using Flame framework just to experience this kind of error. Just build it with the latest package-set and browserify it with your usual tool. It can both show error on client-side disconnect (wss://echo.websocket.org) and server-side handshake error (wss://libwebsockets.org/) https://gist.github.com/chekoopa/90b38915fb7beee49c9241b6a54cbc17

jbrechtel commented 2 years ago

I'm running into this as well on Firefox and Linux.

If you're still interested in addressing this then I can work up a minimal example tomorrow.