UnityWebSocket icon indicating copy to clipboard operation
UnityWebSocket copied to clipboard

How to add a reconnect method :question

Open bomanden opened this issue 6 years ago • 1 comments

How to add a reconnect method if the server is reset or other error occurs ?

bomanden avatar Feb 26 '19 14:02 bomanden

got it working by adding a method to UnityWebsocket.cs

public bool IsAlive() {
  return (_ws == null) ? false : _ws.IsOpen();
}

and having a Coroutine UnityWebSocketScript.cs running from start checking every 15 secs

private IEnumerator ReconnectWS() {
 while (true) {
   yield return new WaitForSeconds(15);
   if (!IsAlive()) {
     Debug.Log("[UnityWebSocketScript] try reconnecting");
     Connect();
   }
 }
}

Good enough ? or any recommendations

bomanden avatar Feb 26 '19 15:02 bomanden