pladaria / reconnecting-websocket

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

Never connects when using reconnect method after forced close #86

Closed outaTiME closed 5 years ago

outaTiME commented 5 years ago

Hi, I've been using you lib for a long time, but today I made some updates in my code to force close on websocket active instance, but when I try to reconnect I have no luck.

Looking the code the _closeCalled flag is still true, probably the reconnect method may set it to false.

https://github.com/pladaria/reconnecting-websocket/blob/master/reconnecting-websocket.ts#L360

I'm using this in react-native environment, basically my code close websocket instance when app goes to background and reconnects when app goes to foreground, so here is the piece of code (websocketConnect method called when app in foreground / first time, websocketDisconnect on background):


const ws = null;

export const websocketConnect = () => async (dispatch, getState) => {
  if (!ws) {
    ws = new ReconnectingWebSocket(Settings.WEBSOCKET_URL, [], {
      debug: __DEV__
    });
    // stubs
  } else {
    ws.reconnect();
  }
};

export const websocketDisconnect = () => async (dispatch, getState) => {
  if (ws) {
    ws.close();
  }
};

Thanks !!!

pladaria commented 5 years ago

Hi,

Thanks for reporting. Fixed in 4.1.6

Let me know if you find any other issue

outaTiME commented 5 years ago

wooow, awesome @pladaria, now it works as expected thanks for your time, really appreciated 🎉

even you add a missing else statement to make disconnection when websocket is already connected.

Thanks once again !!!