PatrickJS/angular-websocket

Reconnecting even after a close()

lionelB opened this issue · 2 comments

Hello,

I'm having an issue using this lib. when I do .close() without any parameter, servers send a frame with no code which is in returned, interpreted by browsers as a 'code:1005' as no code given for closing.
And since the code is not 1000 , the library try to reconnect.

var ws = new WebSocket("ws://echo.websocket.org");
ws.onopen = (e) => console.log(e);
ws.onclose = (e) => console.log(e);
ws.close()
// { target: WebSocket, isTrusted: true, wasClean: true, code: 1005, reason: "", currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, timeStamp: 1467967416163584 }

to retrieve a 1000 code, we have to send it as a close reason. ws.close(1000)
Unfortunatly, the $websocket lib doesn't allow to send a code on close (nor a reason).
I can make a PR for the changes, but I don't know if it's better to check 1000 and 1005 code for normal close, since we can't send code, or, if we add other parameters to the close().

I was able to get around this bug by calling close (with code) on the underlying socket:
this.socket.socket.close(1000, "Client closed socket");

It would be nice to have the library updated to include this.

same issue