pladaria / reconnecting-websocket

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

this._listeners.error is undefined #63

Closed ezraroda closed 6 years ago

ezraroda commented 6 years ago

I'm running into the following scenario, I'm providing incorrect URL on perpouse with 0 retry option. const options = { connectionTimeout: 500, maxRetries: 0, debug: true }; const rws = new ReconnectingWebSocket('ws://**lokalhost**:8080', [], options);

Registering on error event listener:

rws.addEventListener('error' , (evt: any): any => {
                        if (evt.message === 'TIMEOUT') {
                            // do something here
                        }
                    };

Looks like any registered listener is removed on connection error and never get called from the internal _handleError

 _handleError(event) {
            this._debug('error event', event.message);
            this._disconnect(undefined, event.message === 'TIMEOUT' ? 'timeout' : undefined);
            if (this.onerror) {
                this.onerror(event);
            }
            this._debug('exec error listeners');
            **this._listeners.error.forEach(listener => listener(event));**
            this._connect();
        }

getting this error TypeError: this._listeners.error is undefined

Any recommendations how to handle initial connection errors ?

pladaria commented 6 years ago

Hi,

I tried to reproduce your problem:

import ReconnectingWebsocket from "reconnecting-websocket";
import WebSocket from "ws";

const rws = new ReconnectingWebsocket("ws://lokalhost:8080", undefined, {WebSocket});

rws.addEventListener("error", () => {
  console.log("error");
});

This works as expected.

Can you please send a snippet where your issue is reproduced? More details about the library version, environment, etc, would be helpful

lagden commented 6 years ago

@pladaria

The error was reproduced:

<!DOCTYPE html>
<html>
    <head>
        <title>Reconnecting WS</title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <h1>Open console and send a message</h1>
        <pre>echo.send('oohhyeahh!!')</pre>
        <script type="module">
            import ReconnectingWebSocket from 'https://unpkg.com/reconnecting-websocket@4.0.0-rc5/dist/reconnecting-websocket.mjs'

            class Echo {
                constructor() {
                    // Fail
                    this.ws = new ReconnectingWebSocket('wss://echo.websocket.org')

                    // Works
                    // this.ws = new WebSocket('wss://echo.websocket.org')

                    this.ws.addEventListener('open', this)
                    this.ws.addEventListener('message', this)
                    this.ws.addEventListener('close', this)
                    this.ws.addEventListener('error', this)
                }

                send(v = 'ulala') {
                    this.ws.send(v)
                }

                onopen() {
                    console.log('open')
                    this.ws.send('Hello echoooo...')
                }

                onmessage(event) {
                    console.log('message echo --->', event.data)
                }

                onclose() {
                    console.log('close')
                }

                onerror() {
                    console.log('error')
                }

                handleEvent(event) {
                    if (typeof this[`on${event.type}`] === 'function') {
                        this[`on${event.type}`](event)
                    }
                }
            }

            window.echo = new Echo()
        </script>
    </body>
</html>
pladaria commented 6 years ago

@lagden Thanks for reporting! Event handlers were not supporting objects with handleEvent

pladaria commented 6 years ago

@lagden fixed in latest release

@ezraroda closing issue because I was unable to reproduce your problem. Here is my code trying to reproduce it: https://codesandbox.io/s/p4m2707om?expanddevtools=1 Please feel free to reopen this issue if you can reproduce it.

Thank you all for your feedback!