websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

Keep Client Connection Open

Open dommy1C opened this issue 7 years ago • 4 comments

dommy1C avatar Dec 12 '18 14:12 dommy1C

Hey, @dommy1C! Here's a way to reconnect until the connection is available again.

ws.OnClose += (sender, e) =>
{
    Task.Run(() =>
    {
        while (ws.IsAlive == false)
        {
            Thread.Sleep(1000); // time to wait before trying to reconnect in milliseconds
            ws.Connect();
        }
    });
};

nonoesp avatar Mar 11 '19 10:03 nonoesp

Hey, @dommy1C! Here's a way to reconnect until the connection is available again.

ws.OnClose += (sender, e) =>
{
    Task.Run(() =>
    {
        while (ws.IsAlive == false)
        {
            Thread.Sleep(1000); // time to wait before trying to reconnect in milliseconds
            ws.Connect();
        }
    });
};

Instead of Thread.Sleep(1000); You should use Task.Delay(1000).Wait();

sp3tsnaz avatar Apr 25 '19 14:04 sp3tsnaz

Wow, thanks so much! The solution I proposed was giving some problems blocking the app's main thread at times.

nonoesp avatar Apr 25 '19 16:04 nonoesp

Wow, thanks so much! The solution I proposed was giving some problems blocking the app's main thread at times.

Glad it helped. :)

sp3tsnaz avatar Apr 26 '19 07:04 sp3tsnaz