olofd / react-native-signalr

Use SignalR with React Native
150 stars 61 forks source link

Autoreconnect feature #61

Open Gniarke opened 4 years ago

Gniarke commented 4 years ago

Hi,

possible to have the equivalent of js signalr client "@microsoft/signalr" autoreconnect feature ? const connection = new signalR.HubConnectionBuilder() .withUrl("/chatHub") .withAutomaticReconnect() .build(); I didn't find any disconnect catcher, so after a while, when connection is lost, i can't do anything to reconnect the user.

jamesdward commented 4 years ago

Hi @Gniarke

I'd advise looking at the actual SignalR docs

Although a disconnect method isn't mentioned in the react-native-signalr documentation (only on 'connectionSlow'), as it mainly acts as a bridge between react native and jQuery signalR, the other methods also work such as on 'disconnected' and on 'stateChanged' e.g:

connection.disconnected(() => {  
     //connection was fully disconnected   
    });

connection.stateChanged((state) => {
   // Detects any stage change 
   // state = {oldState, newState}
  });

Also a very useful tip - you can access the connection state at any time through connection.state. Status code 1: Connected Status code 2: Reconnecting Status code 4: Disconnected