Ejemplo n.º 1
0
        private void ElectronConnect()
        {
            if (socket != null && socket.State != WebSocketState.Closed)
            {
                Logger.Warn("Multiple attempts to connect web socket");
                return;
            }
            if (socket != null && socket.State == WebSocketState.Connecting)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(serverAddress))
            {
                throw new ArgumentException("IAC is enabled, but no server address was specified.");
            }

            socket = new WebSocket(serverAddress + "/router");

            socket.Opened += (sender, args) =>
            {
                Logger.Info("Web socket connection opened");

                RouterClient = new RouterClient(this, Connect);

                RouterClient.Init();
            };

            socket.Error += (sender, args) =>
            {
                var retryAfter = 100;
                if (socketRetryAttempts >= 20)
                {
                    Logger.Warn("Socket Connection Still Failing after 20 attempts");
                    retryAfter = 500;
                }
                // Keep trying to connect. When large numbers of socket connections are made simultaneously, the connection fails and we get here.
                socketRetryAttempts++;
                isFinsembleConnected = false;
                Disconnected?.Invoke(this, EventArgs.Empty);
                Thread.Sleep(retryAfter);
                ElectronConnect();
            };

            if (socket.State == WebSocketState.Connecting)
            {
                return;
            }

            socket.Open();

            socket.Closed += (s, e) =>
            {
                var retryAfter = 100;
                if (socketRetryAttempts >= 20)
                {
                    Logger.Warn("Socket Connection Still Failing after 20 attempts");
                    retryAfter = 500;
                }
                // Keep trying to connect. When large numbers of socket connections are made simultaneously, the connection fails and we get here.
                socketRetryAttempts++;
                isFinsembleConnected = false;
                Disconnected?.Invoke(this, EventArgs.Empty);
                Thread.Sleep(retryAfter);
                ElectronConnect();
                Console.WriteLine("Web socket connection disconnected.");
            };
        }