Beispiel #1
0
        public void RxTextTest()
        {
            //Arrange
            var objWebSocketClient  = Substitute.For <IWebSocketClient>();
            var objKeepAliveMonitor = Substitute.For <IKeepAliveMonitor>();
            var objClient           = new WebsocketClient(objWebSocketClient, objKeepAliveMonitor);
            var handler             = Substitute.For <EventHandler <byte[]> >();
            var eventArgs           = Substitute.For <IMessageEventArgs>();

            eventArgs.IsText.Returns(true);
            eventArgs.IsPing.Returns(false);
            eventArgs.IsBinary.Returns(false);

            objClient.Rx += handler;
            eventArgs.Data.Returns("TEST");

            //Act
            objClient.ConnectAsync(new ClientParams()
            {
                HostName = "localhost", Port = 80, Secure = false
            });
            objWebSocketClient.OnMessage += Raise.Event <EventHandler <IMessageEventArgs> >(objClient, eventArgs);

            //Assert
            objKeepAliveMonitor.Received(1).Reset();
            handler
            .Received(1)
            .Invoke(Arg.Is <IConnection.IConnection>(m => m.Equals(objClient)), Arg.Is <byte[]>(b => b.SequenceEqual(System.Text.Encoding.UTF32.GetBytes("TEST"))))
            ;
        }
Beispiel #2
0
        public void RxPingTest()
        {
            //Arrange
            var objWebSocketClient  = Substitute.For <IWebSocketClient>();
            var objKeepAliveMonitor = Substitute.For <IKeepAliveMonitor>();
            var objClient           = new WebsocketClient(objWebSocketClient, objKeepAliveMonitor);
            var handler             = Substitute.For <EventHandler <byte[]> >();
            var eventArgs           = Substitute.For <IMessageEventArgs>();

            eventArgs.IsText.Returns(false);
            eventArgs.IsPing.Returns(true);
            eventArgs.IsBinary.Returns(false);

            objClient.Rx += handler;

            //Act
            objClient.ConnectAsync(new ClientParams()
            {
                HostName = "localhost", Port = 80, Secure = false
            });
            objWebSocketClient.OnMessage += Raise.Event <EventHandler <IMessageEventArgs> >(objClient, eventArgs);

            //Assert
            objKeepAliveMonitor.Received(1).Reset();
            handler
            .Received(0)
            .Invoke(Arg.Any <IConnection.IConnection>(), Arg.Any <byte[]>())
            ;
        }
Beispiel #3
0
        public void ConnectAsyncTest()
        {
            //Arrange
            var objWebSocketClient  = Substitute.For <IWebSocketClient>();
            var objKeepAliveMonitor = Substitute.For <IKeepAliveMonitor>();
            var objClient           = new WebsocketClient(objWebSocketClient, objKeepAliveMonitor);

            //Act
            objClient.ConnectAsync(new ClientParams()
            {
                HostName = "localhost",
                Port     = 80,
                Secure   = false,
                ConnectionTimeoutSeconds = 666
            });

            //Assert
            objWebSocketClient.Received(1).OnOpen        += Arg.Any <EventHandler>();
            objWebSocketClient.Received(1).OnClose       += Arg.Any <EventHandler <ICloseEventArgs> >();
            objWebSocketClient.Received(1).OnError       += Arg.Any <EventHandler <IErrorEventArgs> >();
            objWebSocketClient.Received(1).OnMessage     += Arg.Any <EventHandler <IMessageEventArgs> >();
            objKeepAliveMonitor.Received(1).TimeOut      += Arg.Any <EventHandler>();
            objKeepAliveMonitor.Received(1).UnResponsive += Arg.Any <EventHandler>();

            Assert.AreEqual(666, objWebSocketClient.ConnectionTimeoutSeconds);
        }
Beispiel #4
0
        public void DisconnectTest()
        {
            //Arrange
            var objWebSocketClient  = Substitute.For <IWebSocketClient>();
            var objKeepAliveMonitor = Substitute.For <IKeepAliveMonitor>();
            var objClient           = new WebsocketClient(objWebSocketClient, objKeepAliveMonitor);
            var objHandler          = Substitute.For <EventHandler <IConnection.IConnection> >();

            objClient.Disconnected += objHandler;

            //Act
            objClient.ConnectAsync(new ClientParams()
            {
                HostName = "localhost", Port = 80, Secure = false
            });
            objClient.Disconnect();

            //Assert
            objWebSocketClient.Received(1).OnOpen        -= Arg.Any <EventHandler>();
            objWebSocketClient.Received(1).OnClose       -= Arg.Any <EventHandler <ICloseEventArgs> >();
            objWebSocketClient.Received(1).OnError       -= Arg.Any <EventHandler <IErrorEventArgs> >();
            objWebSocketClient.Received(1).OnMessage     -= Arg.Any <EventHandler <IMessageEventArgs> >();
            objKeepAliveMonitor.Received(1).TimeOut      -= Arg.Any <EventHandler>();
            objKeepAliveMonitor.Received(1).UnResponsive -= Arg.Any <EventHandler>();

            objKeepAliveMonitor.Received(1).Stop();

            objHandler
            .Received(1)
            .Invoke(Arg.Is(objClient), Arg.Is(objClient))
            ;
        }
Beispiel #5
0
        static void Websocket(string[] args)
        {
            WebsocketClient ws = new WebsocketClient("localhost:15555");

            ws.BeforeSend = (msg) =>
            {
                msg.Headers["cmd"] = "pub";
                msg.Headers["mq"]  = "MyRpc";
                msg.Headers["ack"] = "false";
            };

            ws.OnOpen += async(client) =>
            {
                IDictionary <string, object> data = new Dictionary <string, object>();
                data["method"] = "plus";
                data["params"] = new object[] { 1, 2 };
                data["module"] = "/";


                for (int i = 0; i < 100; i++)
                {
                    Message msg = new Message
                    {
                        Body = data
                    };

                    var res = await ws.InvokeAsync(msg);

                    Console.WriteLine(JsonKit.SerializeObject(res));
                }
            };

            ws.ConnectAsync();
        }
Beispiel #6
0
        /// <summary>
        /// Starts the XMPP connection
        /// </summary>
        /// <returns></returns>
        public async Task StartAsync()
        {
            await WebsocketClient.ConnectAsync(new Uri("wss://xmpp-service-prod.ol.epicgames.com//"),
                                               CancellationToken.None);

            await OnConnectAsync();

            await OpenAsync();
            await HandleMessagesAsync();
        }
    public void ConnectedTest()
    {
        //Arrange
        var webSocketMock = Substitute.For <IWebSocket>();

        var  subject = new WebsocketClient(webSocketMock);
        bool raised  = false;

        subject.Connected += delegate(object sender, EventArgs e) {
            raised = true;
        };
        subject.ConnectAsync();
        //Act
        webSocketMock.OnOpen += Raise.Event();
        //Assert
        Assert.IsTrue(raised);
    }
Beispiel #8
0
        public void TransmitTest()
        {
            //Arrange
            var objWebSocketClient  = Substitute.For <IWebSocketClient>();
            var objKeepAliveMonitor = Substitute.For <IKeepAliveMonitor>();
            var objClient           = new WebsocketClient(objWebSocketClient, objKeepAliveMonitor);

            objClient.ConnectAsync(new ClientParams()
            {
                HostName = "localhost", Port = 80, Secure = false
            });
            objWebSocketClient.Received().OnOpen += Raise.EventWith(objWebSocketClient, new EventArgs());

            //Act
            objClient.Transmit(new byte[] { 1, 2, 3, 4 });
            Thread.Sleep(50); //-- wait a bit so the sender queue can call the IWebSocketClient.Send() method

            //Assert
            objWebSocketClient.Received(1).SendAsync(Arg.Is <byte[]>(b => b.SequenceEqual(new byte[] { 1, 2, 3, 4 })));
        }
Beispiel #9
0
        public void KeepAliveTimeOutPingOKTest()
        {
            //Arrange
            var objWebSocketClient  = Substitute.For <IWebSocketClient>();
            var objKeepAliveMonitor = Substitute.For <IKeepAliveMonitor>();
            var objClient           = new WebsocketClient(objWebSocketClient, objKeepAliveMonitor);

            objWebSocketClient.Ping().Returns(true);

            //Act
            objClient.ConnectAsync(new ClientParams()
            {
                HostName = "localhost", Port = 80, Secure = false
            });
            objKeepAliveMonitor.TimeOut += Raise.EventWith(this, new EventArgs());

            //Assert
            objKeepAliveMonitor.Received(1).StartCountDown();
            objWebSocketClient.Received(1).Ping();
            objKeepAliveMonitor.Received(1).Reset();
        }
Beispiel #10
0
        public void DisconnectedOnErrorTest(
            [Values(WebSocketState.Closed, WebSocketState.Closing, WebSocketState.Connecting, WebSocketState.Open)] WebSocketState state,
            [Values(false, false, true, true)] bool expectclose
            )
        {
            //Arrange
            var objWebSocketClient  = Substitute.For <IWebSocketClient>();
            var objKeepAliveMonitor = Substitute.For <IKeepAliveMonitor>();
            var objClient           = new WebsocketClient(objWebSocketClient, objKeepAliveMonitor);
            var eventArgs           = Substitute.For <IErrorEventArgs>();
            var handler             = Substitute.For <EventHandler <IConnection.IConnection> >();

            objWebSocketClient.ReadyState.Returns(state);
            objClient.Disconnected += handler;

            //Act
            objClient.ConnectAsync(new ClientParams()
            {
                HostName = "localhost", Port = 80, Secure = false
            });
            objWebSocketClient.OnError += Raise.Event <EventHandler <IErrorEventArgs> >(objClient, eventArgs);

            //Assert
            objWebSocketClient.Received().OnOpen    -= Arg.Any <EventHandler>();
            objWebSocketClient.Received().OnClose   -= Arg.Any <EventHandler <ICloseEventArgs> >();
            objWebSocketClient.Received().OnError   -= Arg.Any <EventHandler <IErrorEventArgs> >();
            objWebSocketClient.Received().OnMessage -= Arg.Any <EventHandler <IMessageEventArgs> >();

            objWebSocketClient.Received((expectclose) ? 1 : 0).Close();

            handler
            .Received(1)
            .Invoke(
                Arg.Is <IConnection.IConnection>(c => c.Equals(objClient)),
                Arg.Is(objClient)
                )
            ;
        }
Beispiel #11
0
        public void ConnectedTest()
        {
            //Arrange
            var objWebSocketClient  = Substitute.For <IWebSocketClient>();
            var objKeepAliveMonitor = Substitute.For <IKeepAliveMonitor>();
            var objClient           = new WebsocketClient(objWebSocketClient, objKeepAliveMonitor);
            var handler             = Substitute.For <EventHandler <IConnection.IClient> >();

            objClient.Connected += handler;

            //Act
            objClient.ConnectAsync(new ClientParams()
            {
                HostName = "localhost", Port = 80, Secure = false
            });
            objWebSocketClient.Received().OnOpen += Raise.EventWith(objWebSocketClient, new EventArgs());

            //Assert
            objKeepAliveMonitor.Received(1).Start();
            handler
            .Received(1)
            .Invoke(Arg.Is(objClient), Arg.Is(objClient))
            ;
        }