pladaria / reconnecting-websocket

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

Unable to connect when manual close and reconnect too fast #87

Closed outaTiME closed 5 years ago

outaTiME commented 5 years ago

Hi again @pladaria, Im unable to get connected when make to fast manual close and reconnect operations the flag _connectLock keep in true and breaks the next connect call.

You cold try the following example:

const ws = null;

const _connect = () => {
  if (!ws) {
    // create one-time websocket instance
    ws = new ReconnectingWebSocket(Settings.WEBSOCKET_URL, [], {
      // https://github.com/pladaria/reconnecting-websocket#available-options
      debug: __DEV__
    });
    // stubs
  } else {
    ws.reconnect();
  }
}

const _disconnect = () => {
  if (ws) {
    ws.close();
  }  
}

// test
_connect();
_disconnect();
_connect();
_disconnect();
_connect();
_disconnect();
_connect(); 
// the connection keeps in wrong state with _connectLock in true and never connect

Thanks !!!

pladaria commented 5 years ago

Thank you for reporting and fixing it!