I am currently using a C# application that needs to connect via websocket to a software that has been coded in C++. The C# application is the client, the C++ software the server. I would like the C# application to try reconnecting to the websocket every 5 seconds if it is not connected. I am currently using websocket-sharp.
This is my code so far :
using System;
using System.Threading;
using WebSocketSharp;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
using (var ws = new WebSocket("ws://192.168.1.25:50000"))
{
ws.OnMessage += (sender, e) =>
{
Console.WriteLine("Message received : " + e.Data);
if (e.Data == "alreadyConnected")
{
ws.Send("forceConnect");
}
if (e.Data == "connexionEstablished")
{
ws.Send("Hello server");
}
};
ws.OnOpen += (sender, e) =>
{
Console.WriteLine("Connexion has been established");
ws.Send("some message");
};
ws.OnClose += (sender, e) =>
{
Console.WriteLine("Connexion has been lost");
if (!e.WasClean)
{
if (!ws.IsAlive)
{
Thread.Sleep(5000);
ws.Connect();
}
}
};
ws.OnError += (sender, e) =>
{
Console.WriteLine("Connexion has led to an error");
};
ws.Connect();
Console.ReadKey(true);
}
}
}
}
But after 10 unsuccessful tries, I get an error message "A series of reconnecting have failed". This is due to a max number of retry fixed in websocket-sharp. After I get this message, I have found no way (nor trying alone neither searching on the internet) to keep on trying to reconnect. Does someone know a way I can do that ?
If someone could help, I'd be very thankful :) Have a nice day !