Problems with WebSocket - Plus Emulator

Cen0n

New Member
Nov 1, 2015
15
0
Hello, I'm having some problems on my WebSocket, could someone help me solve this?
The situation is as follows:

The connection is usually made when the user enters the client, everything works, but when there is an internet oscillation of the person, the connection closes and is disconnected, even if it oscillates for only 1 second.

I was wondering if there is any way to reconnect without being disconnected from the client because all of the interactive functions of the game run through the websocket

Connection code below:
Code:
using Plus;
using Fleck;

namespace WebSocket
{
    public class WebManager
    {
        public WebSocketServer wsServer;
        public WebManager()
        {
            _clientsBySocket = new ConcurrentDictionary<IWebSocketConnection, int>();
            wsServer = new WebSocketServer("ws://0.0.0.0:3000");
        }

        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 && client.GetHabbo() != null && client.GetRolePlay() != null)
                        {
                            _clientsBySocket[ws] = client.GetHabbo().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)
                        return;
                    client.GetRolePlay().ws.Close();
                    client.GetRolePlay().ws = null;
                };
                ws.OnError = Exception => ws.Close();
            });
        }
    }
}
 

Users who are viewing this thread

Top