コード例 #1
0
        public async Task ConnectAsync_Should_Set_IsConnected_To_True()
        {
            using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(DefaultTimeOut * 3));
            _ = StartServer(cts.Token, 9986);
            WSNetworkConnector wsNetworkConnector = await StartClient(cts.Token, 9986);

            Assert.IsTrue(wsNetworkConnector.IsConnected);
            cts.Cancel();
        }
コード例 #2
0
        private async Task <WSNetworkConnector> StartClient(CancellationToken cancellationToken, int port)
        {
            WSNetworkConnector wsNetworkConnector = new WSNetworkConnector(MessageTypeCache, MessageSerializer, MessageProcessor, Host, port, "glovali");
            await wsNetworkConnector.ConnectAsync(cancellationToken);

            await wsNetworkConnector.StartHandshakeAsClientAsync();

            wsNetworkConnector.Start();
            return(wsNetworkConnector);
        }
コード例 #3
0
        public async Task SendMessageAsync_Should_Invoke_MessageProcessor()
        {
            using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(DefaultTimeOut * 3));
            _ = StartServer(cts.Token, 9987);
            TestRequestMessage message = new TestRequestMessage()
            {
                Message = "Test123"
            };
            WSNetworkConnector wsNetworkConnector = await StartClient(cts.Token, 9987);

            await wsNetworkConnector.SendMessageAsync(message, cts.Token);

            IMessage messageInMessageProcessor = await MessageProcessor.GetMessageAsync(cts.Token);

            Assert.AreEqual(message.Id, messageInMessageProcessor?.Id);
            cts.Cancel();
        }
コード例 #4
0
        public async Task SendMessageAsync_Should_Invoke_MessageProcessor()
        {
            using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(DefaultTimeOut * 3));
            _ = StartServer(cts.Token, 9987);
            AuthenticateRequest message = new AuthenticateRequest()
            {
                CredentialTypeCode = "Name", Password = "******", Username = "******"
            };
            WSNetworkConnector wsNetworkConnector = await StartClient(cts.Token, 9987);

            await wsNetworkConnector.SendMessageAsync(message, cts.Token);

            IMessage messageInMessageProcessor = await MessageProcessor.GetMessageAsync(cts.Token);

            Assert.AreEqual(message.Id, messageInMessageProcessor?.Id);
            cts.Cancel();
        }
コード例 #5
0
        private async Task StartServer(CancellationToken cancellationToken, int port)
        {
            Console.WriteLine("Starting listener...");
            TcpListener tcpListener = new TcpListener(IPAddress.Loopback, port);

            tcpListener.Start();
            Console.WriteLine("Started listener!");
            while (!cancellationToken.IsCancellationRequested)
            {
                Console.WriteLine("Awaiting new connection...");
                TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync();

                Console.WriteLine("Accepted connection!");
                _ = Task.Run(async() =>
                {
                    WSNetworkConnector networkConnector = new WSNetworkConnector(MessageTypeCache, MessageSerializer, MessageProcessor, tcpClient, "glovali");
                    await networkConnector.StartHandshakeAsServerAsync();
                    networkConnector.Start();
                    Console.WriteLine("Started websocket network connector");
                }, cancellationToken);
            }
        }