Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this._listeners.error is undefined #63

Closed
ezraroda opened this issue May 30, 2018 · 4 comments
Closed

this._listeners.error is undefined #63

ezraroda opened this issue May 30, 2018 · 4 comments

Comments

@ezraroda
Copy link

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
Copy link
Owner

pladaria commented Jun 4, 2018

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
Copy link

lagden commented Jun 24, 2018

@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
Copy link
Owner

pladaria commented Sep 29, 2018

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

@pladaria
Copy link
Owner

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants