Hello, I'm having some problems with the WebSocket connection from my game, could anyone tell me any solution?
What happens: A user is usually in the game, when his connection oscillates even for 1 second (ping does not respond) the connection shortens and it is disconnected, is there any way to reconnect automatically without it falling out of the game?
Code:
private readonly ConcurrentDictionary<IWebSocketConnection, int> _clientsBySocket;
public void Init()
{
wsServer.Start(ws =>
{
ws.OnOpen = () => ws.Send("searching");
ws.OnMessage = msg =>
{
if (string.IsNullOrEmpty(msg))
return;
var evnt = Regex.Split(msg, "Event")[1];
var Evnt = evnt.Split('.')[0];
var name = Regex.Split(msg, "Name")[1];
var Name = name.Split('.')[0];
var data = Regex.Split(msg, "Data")[1];
var Data = data.Split('.')[0];
var edata = Regex.Split(msg, "ExtraData")[1];
var ExtraData = edata.Split('.')[0];
var client = PlusEnvironment.GetGame().GetClientManager().GetClientByUsername(Name);
if (Evnt == "search")
{
if (client != null)
{
_clientsBySocket[ws] = client.Id;
client.GetRolePlay().ws = ws;
}
else
{
ws.Send("searching");
}
}
else
{
client.GetRolePlay().WebHandle(Evnt, Data, ExtraData);
}
};
ws.OnClose = () =>
{
if (!_clientsBySocket.TryGetValue(ws, out var clientId)) return;
var client = PlusEnvironment.GetGame().GetClientManager().GetClientByUserID(clientId);
if (client?.GetRolePlay().ws == null || client?.GetRolePlay()?.ws == null || client.GetRolePlay().ws == null)
return;
client.GetRolePlay().ws.Close();
client.GetRolePlay().ws = null;
ConsoleWriter.Writer.WriteLine("conexão de " + client.GetHabbo().Username + " fechada!", System.ConsoleColor.White);
};
ws.OnError = Exception => ConsoleWriter.Writer.Desconexoes("Exception salva: " + Exception.ToString() + ".");
ws.OnError = Exception => ws.Close();
});
}
Citação