reconnecting-websocket icon indicating copy to clipboard operation
reconnecting-websocket copied to clipboard

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

Open masx200 opened this issue 5 years ago • 2 comments

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 avatar Apr 10 '20 12:04 masx200


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.")
        }
    }

masx200 avatar Apr 10 '20 12:04 masx200

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.

EndlessDex avatar Oct 19 '20 18:10 EndlessDex