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

Keep connection closed after close() #14

Merged
merged 1 commit into from
May 20, 2017
Merged

Keep connection closed after close() #14

merged 1 commit into from
May 20, 2017

Conversation

PaulSD
Copy link
Contributor

@PaulSD PaulSD commented May 19, 2017

If close() is called after handleClose() and before connect() (while the underlying WebSocket is closed), the WebSocket will be reopened again after the reconnectDelay timeout expires.

To prevent this, check shouldRetry after the reconnectDelay timeout expires to determine whether the WebSocket should be reopened.

My particular use case is: I would like to stop reconnecting if a particular WebSocket Close Status Code is received from the server. The obvious solution was:

var checkClose = function(event) {
  if(event.code == 4000) {
    rcws.removeEventListener('close', checkClose);
    rcws.close(event.code, undefined, {keepClosed: true, fastClose: true});
  }
};
rcws.addEventListener('close', checkClose);

However, this doesn't work as expected because handleClose() is called before my checkClose() function and there is no way for me to cancel the reconnectDelay timer to prevent ReconnectingWebsocket from reconnecting after I call rcws.close(). This commit fixes this issue.

If close() is called after handleClose() and before connect() (while the
underlying WebSocket is closed), the WebSocket will be reopened again
after the reconnectDelay timeout expires.

To prevent this, check shouldRetry after the reconnectDelay timeout
expires to determine whether the WebSocket should be reopened.
@pladaria pladaria merged commit 4655f3d into pladaria:master May 20, 2017
pladaria added a commit that referenced this pull request May 20, 2017
@pladaria
Copy link
Owner

Thank you! Good catch. Will add test coverage and release a new npm version

pladaria added a commit that referenced this pull request May 20, 2017
@pladaria
Copy link
Owner

published reconnecting-websocket@3.0.5

@msageryd
Copy link

Thank you @PaulSD for your solution and contribution. This was exactly what I needed to kick out users with expired tokens.

Also, big thumbs up for @pladaria for a great library. I've tried out a bunch of libraries for React Native. This is by far my favourite.

@jiajunxiong jiajunxiong mentioned this pull request May 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants