Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to realize disconnection reconnection? #720

Open
Naylor55 opened this issue Jan 28, 2023 · 1 comment
Open

How to realize disconnection reconnection? #720

Naylor55 opened this issue Jan 28, 2023 · 1 comment

Comments

@Naylor55
Copy link

How to realize disconnection reconnection? We now encounter the following scenario: After the server is restarted, the client loses its connection, and they cannot continue to communicate unless the client is restarted.

@Naylor55
Copy link
Author

When the websocket-server is restarted, the client will lose the connection with the server. If the reconnection mechanism is not introduced, the business data will not be updated in real time until the client is restarted manually. This is definitely not allowed in actual use, so the reconnection mechanism needs to be introduced. After snapping to the connection closing, try to reconnect.

The number of retries is 5, and the retry interval is 5s. The example code is as follows:


 //连接关闭
wsLabel.OnClose += (sender, e) =>
{
    OnWsLabelClose(sender, e);
};


......


int retry = 0;
/// <summary>
/// 连接关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnWsLabelClose(object sender, CloseEventArgs e)
{
    Log.LogInfo.Info("OnWsLabelClose,retry=" + retry);
    if (retry < 5)
    {
        retry++;
        Thread.Sleep(5000);
        wsLabel.Connect();
    }
    else
    {
        wsLabel.Log.Error("The reconnecting has failed.");
        Log.LogInfo.Info("重新连接失败,超过最大重试次数");
    }
}


        

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant