pladaria / reconnecting-websocket

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

provide current retried count #119

Open nttoan26 opened 4 years ago

nttoan26 commented 4 years ago

How can I know whether a retrying is the last time or not, so I can display an error msg to user ? If there is no way then this should be an enhancement.

Updated: socket._retryCount get increased on every retrying except the last time, so it's useless

   if (this._retryCount >= maxRetries) {
         this._debug('max retries reached', this._retryCount, '>=', maxRetries);
         return;
     }
    this._retryCount++;
nttoan26 commented 4 years ago

Work-around solution: call socket.close() on (maxRetries - 1) retrying attempt to prevent last attempt

  socket.addEventListener('error', (e) => {
      console.log('[WS] error', socket.retryCount);
      if (socket.retryCount >= maxRetries) {
        store.dispatch({ type: Action.ACTION_SHOW_ERROR, msg: 'Notifications are unavailable' });
        socket.close();
        socket = null;
      }
    });

hence set maxRetries = desired maxRetries + 1

const maxRetries = 3; const options = { maxRetries: maxRetries + 1 };