コード例 #1
0
        public async Task TestCreateServerClient_connect_2_boot_1()
        {
            //arrange
            var server = new WebListenerWebSocketServer();
            var port   = _FreeTcpPort();

            var beh = new testBeh()
            {
            };

            beh.StringMessageHandler = (e) =>
            {
                e.WebSocket.SendStringAsync(e.Data + e.Data, CancellationToken.None);
            };

            server.AddRouteBehavior("/aaa", () => beh);
            await server.StartAsync($"http://localhost:{port}/");

            string res        = null;
            string kickoffRes = null;

            var oldClient = new WebSocketClient()
            {
                MessageHandler                         = (e) => res = e.Data,
                CloseHandler                           = (e) =>
                                            kickoffRes = e.CloseStatDescription
            };
            await oldClient.ConnectAsync($"ws://localhost:{port}/aaa");

            await oldClient.SendStringAsync("hi", CancellationToken.None);

            await Task.Delay(100);

            var  clients         = server.GetActiveConnectionIds();
            Guid oldConnectionId = clients.First();

            for (int i = 0; i < 100; i++)
            {
                var newClient = new WebSocketClient()
                {
                    MessageHandler                         = (e) => res = e.Data,
                    CloseHandler                           = (e) =>
                                                kickoffRes = e.CloseStatDescription
                };
                await newClient.ConnectAsync($"ws://localhost:{port}/aaa");

                await newClient.SendStringAsync("hi", CancellationToken.None);

                await Task.Delay(100);

                var  newClients      = server.GetActiveConnectionIds();
                Guid newConnectionId = newClients.Where(id => id != oldConnectionId).First();
                Assert.Equal(2, newClients.Count);

                //Disconnect old client
                await server.DisconnectConnection(oldConnectionId, "dontlikeu");

                await Task.Delay(100);

                newClients = server.GetActiveConnectionIds();
                Assert.Equal(1, newClients.Count);
                Assert.Equal(newConnectionId, newClients.First());

                //Verify that new client still works
                await newClient.SendStringAsync("hi", CancellationToken.None);

                //Verify that old client doesn't work
                try
                {
                    await oldClient.SendStringAsync("do you like me?", CancellationToken.None);
                }
                catch (Exception e)
                {
                    string message = e.ToString();
                }

                //Verify that new client still works again
                await newClient.SendStringAsync("hi", CancellationToken.None);

                while (kickoffRes == null)
                {
                    await Task.Delay(100);
                }
                Assert.Equal("dontlikeu", kickoffRes);
                Assert.Equal("hihi", res);

                oldClient       = newClient;
                oldConnectionId = newConnectionId;
            }
        }