sta / sta/websocket-sharp

How to correctly check connection alive and try to reconnect every time if connection is lost in any way.

Open
#224 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

My script connect with the websocket server and receive data. I want to keep this connection alive forever and try to reconnect every time if server is restarting or client lost connection etc.

My script looks like below. It works fine but every time if connection is lost it freeze main thread for few seconds.

Is this a correct and best way to do this?

using UnityEngine;
using System;
using System.Collections;
using WebSocketSharp;
using WebSocketSharp.Net;

public class TestConnection  :  MonoBehaviour  {


    private WebSocket ws;
    private IEnumerator checkConnectionAlive;


    void  Start(){
        checkConnectionAlive = CheckConnectionAlive ();
        Connect ();
    }


    IEnumerator CheckConnectionAlive()
    {
        while (true) {
            Debug.Log ("checking connection");
            if (ws.IsAlive == true) {
                Debug.Log ("connected");
            } else {
                Debug.Log ("not connected");
                Disconnect ();
                Connect ();

            }

            yield return new WaitForSeconds(5);
        }
    }

    void  OnApplicationQuit()  { 
        Disconnect(); 
        StopCoroutine (checkConnectionAlive);
    }

    void  Connect()  {

        ws = new WebSocket ("ws://192.168.4.1:80");

        ws.OnOpen +=  ( sender,  e )  =>  { 
            Debug.Log  ( "WebSocket Open" ); 
        };
        ws.EmitOnPing = true;
        ws.OnMessage +=  ( sender ,  e )  =>  { 

            // handling data here

        };

        ws.OnError +=  ( sender ,  e )  =>  { 
            Debug.Log  ( "WebSocket Error Message:"  +  e.Message ); 
        };

        ws.OnClose +=  ( sender ,  e )  =>  { 
            Debug.Log  ( "WebSocket Close" + e.Reason + " --- " + e.Code ); 

        };

        ConnectF ();


    }
    private void ConnectF() {
        ws.ConnectAsync ();
        StartCoroutine(checkConnectionAlive);
    }

    void Disconnect()  { 
        ws.Close  (); 
        ws = null ; 
    }

    void Send(string message)  { 
        ws.Send(message); 
    }


} 

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the CheckConnectionAlive coroutine and the ConnectF, Disconnect, and ConnectAsync entry points in the issue's example. Reproduce the reported freeze by stopping or restarting the WebSocket server and by dropping the client connection. Done means the reconnect behavior and the cause of the main-thread pause are clearly established, with a documented recommendation for this usage pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.