public void ShouldReconnectByDefault()
        {
            var log = LogManager.GetLogger(GlobalHelper.CallerName());

            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag             = false;

            var options = CreateOptions();
            var uri     = CreateUri();

            var manager = new Manager(new Uri(uri), options);

            socket = manager.Socket("/");

            manager.On(SOCKETNET35.Socket.EVENT_RECONNECT, () =>
            {
                log.Info("EVENT_RECONNECT");
                Flag = true;
                socket.Disconnect();
                ManualResetEvent.Set();
            });

            //await Task.Delay(500);
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
            log.Info("before EngineSocket close");
            manager.EngineSocket.Close();

            ManualResetEvent.WaitOne(15000);
            Assert.True(Flag);
        }
        public void ConnectDisconnect_ShouldReturnCorrectEvents()
        {
            var log = LogManager.GetLogger(GlobalHelper.CallerName());

            log.Info("Start ConnectDisconnect_ShouldReturnCorrectEvents");
            ManualResetEvent = new ManualResetEvent(false);

            var options = CreateOptions();
            var uri     = CreateUri();

            socket = SOCKETNET35.IO.Socket(ServerUrl);
            socket.On(SOCKETNET35.Socket.EVENT_CONNECT, () =>
            {
                log.Info("EVENT_CONNECT");
                socket.Disconnect();
            });

            socket.On(SOCKETNET35.Socket.EVENT_DISCONNECT,
                      (data) =>
            {
                log.Info("EVENT_DISCONNECT");
                Message = (string)data;
                ManualResetEvent.Set();
            });

            //socket.Open();
            ManualResetEvent.WaitOne(15000);
            socket.Close();
            Assert.Equal("io client disconnect", this.Message);
        }
    public void DisconnectSocket()
    {
        if (Connected == false)
        {
            MessageBroker.Default.Publish(new MessageEventArgs()
            {
                Message = "Socket not connected. Ignoring disconnect."
            });
            return;
        }

        Connected = false;
        Debug.Log("SocketService disconnected from client: " + Id.ToString());
        MainSocket.Disconnect();
        MainSocket.Close();
        //MainSocket = null;
        MessageBroker.Default.Publish(new MessageEventArgs()
        {
            Message = "Socket disconnected."
        });
    }
        public void ShouldOpenANewNamespaceAfterConnectionGetsClosed()
        {
            var log = LogManager.GetLogger(GlobalHelper.CallerName());

            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag             = false;

            var options = CreateOptions();
            var uri     = CreateUri();

            var manager = new Manager(new Uri(uri), options);

            socket = manager.Socket("/");

            socket.On(SOCKETNET35.Socket.EVENT_CONNECT, () =>
            {
                socket.Disconnect();
            });

            socket.On(SOCKETNET35.Socket.EVENT_DISCONNECT, () =>
            {
                var foo = manager.Socket("/foo");
                foo.On(SOCKETNET35.Socket.EVENT_CONNECT, () =>
                {
                    Flag = true;
                    foo.Disconnect();
                    socket.Disconnect();
                    manager.Close();
                    ManualResetEvent.Set();
                });
                foo.Open();
            });
            var result = ManualResetEvent.WaitOne(15000);

            Assert.True(Flag);
            socket.Close();
        }
        public void ConnectDisconnectConnect_ShouldReturnCorrectEvents()
        {
            var log = LogManager.GetLogger(GlobalHelper.CallerName());

            log.Info("Start ConnectDisconnectConnect_ShouldReturnCorrectEvents");
            ManualResetEvent = new ManualResetEvent(false);
            var connectCount    = 0;
            var disconnectCount = 0;

            var options = CreateOptions();
            var uri     = CreateUri();

            socket = SOCKETNET35.IO.Socket(ServerUrl, new IO.Options()
            {
                AutoConnect = false
            });

            socket.On(SOCKETNET35.Socket.EVENT_CONNECT, () =>
            {
                connectCount++;
                log.Info("EVENT_CONNECT");
                socket.Disconnect();
            });

            socket.On(SOCKETNET35.Socket.EVENT_DISCONNECT,
                      (data) =>
            {
                disconnectCount++;
                log.Info("EVENT_DISCONNECT");
                Message = (string)data;

                if (connectCount < 2)
                {
                    log.Info("Connecting count: " + connectCount);
                    socket.Connect();
                }
                else
                {
                    ManualResetEvent.Set();
                }
            });

            socket.Connect();
            ManualResetEvent.WaitOne(65000);
            Assert.Equal(2, connectCount);
            Assert.Equal(2, disconnectCount);
            socket.Close();
            Assert.Equal("io client disconnect", this.Message);
        }
        public void ShouldTryToReconnectTwiceAndFailWhenRequestedTwoAttemptsWithIncorrectAddressAndReconnectEnabled()
        {
            var log = LogManager.GetLogger(GlobalHelper.CallerName());

            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag             = false;
            var reconnects = 0;

            var options = CreateOptions();

            options.Reconnection         = true;
            options.ReconnectionAttempts = 2;
            options.ReconnectionDelay    = 10;
            var uri = "http://localhost:3940";


            var manager = new Manager(new Uri(uri), options);

            socket = manager.Socket("/asd");

            manager.On(Manager.EVENT_RECONNECT_ATTEMPT, () =>
            {
                log.Info("EVENT_RECONNECT_ATTEMPT");
                reconnects++;
            });

            manager.On(Manager.EVENT_RECONNECT_FAILED, () =>
            {
                log.Info("EVENT_RECONNECT_FAILED");
                Flag = true;
                socket.Disconnect();
                manager.Close();
                ManualResetEvent.Set();
            });

            ManualResetEvent.WaitOne(15000);
            Assert.Equal(2, reconnects);
        }
        public void ShouldNotTryToReconnectWithIncorrectPortWhenReconnectionDisabled()
        {
            var log = LogManager.GetLogger(GlobalHelper.CallerName());

            log.Info("Start");
            ManualResetEvent = new ManualResetEvent(false);
            Flag             = false;


            var options = CreateOptions();

            options.Reconnection = false;
            var uri = "http://localhost:3940";

            var manager = new Manager(new Uri(uri), options);

            socket = manager.Socket("/invalid");

            manager.On(Manager.EVENT_RECONNECT_ATTEMPT, () =>
            {
                Flag = true;
            });

            manager.On(Manager.EVENT_CONNECT_ERROR, () =>
            {
                // set a timeout to let reconnection possibly fire
                log.Info("EVENT_CONNECT_ERROR");
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
                ManualResetEvent.Set();
            });

            ManualResetEvent.WaitOne(15000);
            log.Info("after WaitOne");
            socket.Disconnect();
            manager.Close();
            Assert.False(Flag);
        }