首页 > 解决方案 > websocketsharp opens multiple concurrent connections in same client

问题描述

I have a client/server application, that works by sending/receiving commands, and if the connections is not open, it shall attempt to reconnect and send the command.

The problem is, the client opens multiple connections and send the same command over all of them, so the server ends up receiving 4, 5, 10+ same commands from the same client, which causes to send a response x10 times to the client.

Also, if the connection drops/socket closes, it should attempt to reconnect, but, instead it will try to open infinite amounts of connections concurrent.

This is a sample of connection and single command.

        public void Start(int attempt = 0)
        {
            try
            {
                if (!ws.IsAlive)
                {
                    ws.OnMessage += Ws_OnMessage;
                    DisableLog.Disable(ws.Log);
                    ws.Connect();
                    if ((!ws.IsAlive) && attempt <= 10) { AddLog(1, $"{attempt}/10 No connection to remote server could be made."); ws.Close(); Thread.Sleep(5000); Start(++attempt); }
                    else if ((!ws.IsAlive) && attempt > 10) { AddLog(1, "Unable to connect to the server. Program will restart."); ws.Close(); Program.restart(); }
                    ws.OnClose += Ws_OnClose;
                }
            }
            catch
            {
            }
        }
        private void Ws_OnClose(object sender, CloseEventArgs e)
        {
            try
            {
                if (!e.WasClean)
                {
                    ws.Close();
                    Start();
                }
            } catch { }
        }
        public bool Set(int x = 0)
        {
            try
            {
                if (x > 5) { AddLog(1, "Socket error"); return false; }
                else
                {
                    if (ws.IsAlive) { ws.Send($"{command}"); }
                    else { Start(); Set(++x); }
                    string data = checkResponse("set");
                    string[] decode = data.Split(',');
                    if (decode[0] != "error") { return true; } else { return false; }
                }
            } catch { return false; }
        }
        static void Main(string[] args)
        {
            Start();
            if (Set()) Console.WriteLine("command successful");
        }    ​

In those scenarios:

a) the socket connection is lost, it will attempt to reconnect max 10 times, but, this is instead the output:

0/10 No connection to remote server could be made.
1/10 No connection to remote server could be made.
0/10 No connection to remote server could be made.
2/10 No connection to remote server could be made.
1/10 No connection to remote server could be made.
0/10 No connection to remote server could be made.
3/10 No connection to remote server could be made.
1/10 No connection to remote server could be made.
4/10 No connection to remote server could be made.
2/10 No connection to remote server could be made.
0/10 No connection to remote server could be made.
3/10 No connection to remote server could be made.
-> it goes on infinite amount of times multiple/concurrent

b) the command is being tried to send, but it fails due to connection, then it will attempt to reconnect and send again, but instead it opens multiple concurrent connections instead of a single one, and thus the output is this:

Socket Error
command successful
command successful
command successful
command successful
command successful
Received response from server 1
Received response from server 1
Received response from server 1
Received response from server 1
Received response from server 1
-> this should happen only once, but it's being spammed multiple times

How do I stop this behavior?

标签: c#websocketwebsocket-sharp

解决方案


推荐阅读