joewalnes / reconnecting-websocket

A small decorator for the JavaScript WebSocket API that automatically reconnects
MIT License
4.21k stars 968 forks source link

onclose() event object is missing code, reason, & wasClean #75

Open jaredevantabor opened 7 years ago

jaredevantabor commented 7 years ago

The onclose() event object is missing keys like code, reason, and wasClean. After some investigation I realized that the event returned to the user is a CustomEvent instead of the CloseEvent returned by native sockets. It looks like in line 266 you are creating a custom event:

if (!reconnectAttempt && !timedOut) {
        if (self.debug || ReconnectingWebSocket.debugAll) {
                   console.debug('ReconnectingWebSocket', 'onclose', self.url);
        }
        eventTarget.dispatchEvent(generateEvent('close')); //<== creating custom event
}

In doing this, the keys, event.code, event.reason, and event.wasClean are stripped and essentially lost in translation, giving the user no useful information to the reason the connection was closed.

For some background, I'm hoping to use these keys to determine if the connection was closed on purpose or due to some connection issue, and to re-send the last message if there was an issue with the connection.

nathanboktae commented 7 years ago

robust-websocket fowards the real CloseEvent if you'd like to switch over to a library with tests and more features.

catamphetamine commented 7 years ago

@nathanboktae Good thing people leave comments like that. I'm usually browsing through the open issues before using a library to check its health status.