pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.23k stars 199 forks source link

The `send ()` method is inconsistent with the websocket standard. When the connection is not in the `open` state, an error should be reported. #129

Open masx200 opened 4 years ago

masx200 commented 4 years ago

The send () method is inconsistent with the websocket standard. When the connection is not in the open state, an error should be reported.

https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send

Exceptions thrown

INVALID_STATE_ERR

The connection is not currently OPEN.

masx200 commented 4 years ago

public send(data: Message) {
        if (this._ws && this._ws.readyState === this.OPEN) {
            this._debug('send', data);
            this._ws.send(data);
        } else {
            const {maxEnqueuedMessages = DEFAULT.maxEnqueuedMessages} = this._options;
            if (this._messageQueue.length < maxEnqueuedMessages) {
                this._debug('enqueue', data);
                this._messageQueue.push(data);
            }
throw Error("The connection is not currently OPEN.")
        }
    }
EndlessDex commented 4 years ago

One of the main features of this library is that it masks brief disconnects and queues up messages to be sent when reconnected. If you want an error on a send failure then you should be using the standard websocket API.