示例#1
0
        public async Task T01_DisposeBeforeReceive()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            client.Dispose();
            Assert.Empty(await client.ReceiveObservable.ToList());
        }
示例#2
0
文件: Examples.cs 项目: KAW0/RxSocket
        public async Task T00_SendAndReceiveStringMessage()
        {
            // Create a socket server on the endpoint.
            var server = RxSocketServer.Create(IPEndPoint, SocketServerLogger);

            // Start a task to allow the server to accept the next client connection.
            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();

            // Create a socket client by successfully connecting to the server at EndPoint.
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            // Get the client socket accepted by the server.
            var accept = await acceptTask;

            Assert.True(accept.Connected && client.Connected);

            // start a task to receive the first string from the server.
            var dataTask = client.ReceiveObservable.ToStrings().FirstAsync().ToTask();

            // The server sends a string to the client.
            accept.Send("Welcome!".ToByteArray());
            Assert.Equal("Welcome!", await dataTask);

            client.Dispose();
            server.Dispose();
        }
示例#3
0
文件: Examples.cs 项目: KAW0/RxSocket
        public async Task T00_Example()
        {
            // Create a socket server on the Endpoint.
            var server = RxSocketServer.Create(IPEndPoint);

            // Start accepting connections from clients.
            server.AcceptObservable.Subscribe(acceptClient =>
            {
                acceptClient.ReceiveObservable.ToStrings().Subscribe(onNext: message =>
                {
                    // Echo each message received back to the client.
                    acceptClient.Send(message.ToByteArray());
                });
            });

            // Create a socket client by connecting to the server at EndPoint.
            var client = await RxSocketClient.ConnectAsync(IPEndPoint);

            client.ReceiveObservable.ToStrings().Subscribe(onNext: message =>
            {
                Assert.Equal("Hello!", message);
            });

            client.Send("Hello!".ToByteArray());

            await Task.Delay(100);

            client.Dispose();
            server.Dispose();
        }
示例#4
0
文件: Examples.cs 项目: KAW0/RxSocket
        public async Task T30_Both()
        {
            var server = RxSocketServer.Create(IPEndPoint, SocketServerLogger); //.AddDisconnectableTo(disconnectables);

            server.AcceptObservable.Subscribe(accepted =>
            {
                "Welcome!".ToByteArray().SendTo(accepted);

                accepted
                .ReceiveObservable
                .ToStrings()
                .Subscribe(s => s.ToByteArray().SendTo(accepted));
            });

            var clients = new List <IRxSocketClient>();

            for (var i = 0; i < 10; i++)
            {
                var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

                client.Send("Hello".ToByteArray());
                clients.Add(client);
            }

            foreach (var client in clients)
            {
                Assert.Equal("Hello", await client.ReceiveObservable.ToStrings().Skip(1).Take(1).FirstAsync());
            }

            foreach (var client in clients)
            {
                client.Dispose();
            }
            server.Dispose();
        }
示例#5
0
        public async Task T05_DisposeBeforeSend()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            client.Dispose();
            Assert.Throws <ObjectDisposedException>(() => client.Send(new byte[] { 0 }));
        }
示例#6
0
        public async Task T00_0Ok()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var accept = await AcceptTask;

            client.Dispose();
        }
示例#7
0
        public async Task T02_DisposeDuringReceive()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var receiveTask = client.ReceiveObservable.LastOrDefaultAsync().ToTask();

            client.Dispose();
            await receiveTask;
        }
示例#8
0
        public async Task T03_ExternalDisposeBeforeReceive()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var accept = await AcceptTask;

            accept.Dispose();
            await client.ReceiveObservable.LastOrDefaultAsync();

            client.Dispose();
        }
示例#9
0
        public async Task T07_ExternalDisposeBeforeSend()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var accept = await AcceptTask;

            accept.Dispose();
            client.Send(new byte[] { 0, 1, 2, 3 });
            Assert.Throws <SocketException>(() => client.Send(new byte[] { 0, 1, 2, 3 }));
            client.Dispose();
        }
示例#10
0
        public async Task T04_ExternalDisposeDuringReceive()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var accept      = await AcceptTask;
            var receiveTask = client.ReceiveObservable.FirstAsync().ToTask();

            accept.Dispose();
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await receiveTask);

            client.Dispose();
        }
示例#11
0
        public async Task T02_ReceiveStringsFromPrefixedBytes()
        {
            //var server = RxSocketServer.Create(IPEndPoint, SocketServerLogger);
            var server     = RxSocketServer.Create(IPEndPoint);
            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();

            //var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);
            var client = await RxSocketClient.ConnectAsync(IPEndPoint);

            Assert.True(client.Connected);

            var countTask = client.ReceiveObservable.ToByteArrayOfLengthPrefix().ToStringArray().Count().ToTask();

            var accept = await acceptTask;

            Assert.True(accept.Connected);

            var message = new [] { "Welcome!" }.ToByteArrayWithLengthPrefix();

            var watch = new Stopwatch();

            watch.Start();

            for (var i = 0; i < messages; i++)
            {
                accept.Send(message);
            }
            accept.Dispose();

            int count = await countTask;

            watch.Stop();

            Assert.Equal(messages, count);

            var frequency = Stopwatch.Frequency * messages / watch.ElapsedTicks;

            Write($"{frequency:N0} messages / second");

            client.Dispose();
            server.Dispose();
        }
示例#12
0
文件: Examples.cs 项目: KAW0/RxSocket
        public async Task T10_ReceiveObservable()
        {
            var server     = RxSocketServer.Create(IPEndPoint, SocketServerLogger);
            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();
            var client     = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var accept = await acceptTask;

            Assert.True(accept.Connected && client.Connected);

            client.ReceiveObservable.ToStrings().Subscribe(str =>
            {
                Write(str);
            });

            accept.Send("Welcome!".ToByteArray());
            "Welcome Again!".ToByteArray().SendTo(accept); // Note SendTo() extension method.

            client.Dispose();
            server.Dispose();
        }
示例#13
0
        public async Task T01_ReceiveStrings()
        {
            var server     = RxSocketServer.Create(IPEndPoint);
            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();

            var client = await RxSocketClient.ConnectAsync(IPEndPoint);

            Assert.True(client.Connected);
            var countTask = client.ReceiveObservable.ToStrings().Count().ToTask();

            var accept = await acceptTask;

            Assert.True(accept.Connected);

            var watch = new Stopwatch();

            watch.Start();

            // send messages from server to client
            var message = "Welcome!".ToByteArray();

            for (var i = 0; i < messages; i++)
            {
                accept.Send(message);
            }
            accept.Dispose();

            var count = await countTask;

            watch.Stop();

            Assert.Equal(messages, count);

            var frequency = Stopwatch.Frequency * messages / watch.ElapsedTicks;

            Write($"{frequency:N0} messages / second");

            client.Dispose();
            server.Dispose();
        }
示例#14
0
文件: Examples.cs 项目: KAW0/RxSocket
        public async Task T20_AcceptObservable()
        {
            var server = RxSocketServer.Create(IPEndPoint, SocketServerLogger);

            server.AcceptObservable.Subscribe(accepted =>
            {
                "Welcome!".ToByteArray().SendTo(accepted);
            });

            var client1 = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var client2 = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var client3 = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            Assert.Equal("Welcome!", await client1.ReceiveObservable.ToStrings().Take(1).FirstAsync());
            Assert.Equal("Welcome!", await client2.ReceiveObservable.ToStrings().Take(1).FirstAsync());
            Assert.Equal("Welcome!", await client3.ReceiveObservable.ToStrings().Take(1).FirstAsync());

            client1.Dispose();
            client2.Dispose();
            client3.Dispose();
            server.Dispose();
        }
示例#15
0
 public async Task T00_Cancel()
 {
     await Assert.ThrowsAsync <OperationCanceledException>(async() =>
                                                           await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger, ct: new CancellationToken(true)));
 }
示例#16
0
        public async Task T01_Disc()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint);

            var accept = await AcceptTask;


            //var firstString = await accept.ReceiveObservable.ObserveOn(NewThreadScheduler.Default);

            NewThreadScheduler.Default.Schedule(async() =>
            {
                var firstString = await accept.ReceiveObservable.ToStrings().FirstAsync();
                if (firstString != "A")
                {
                    throw new InvalidDataException("'A' not received.");
                }

                /*
                 * accept.ReceiveObservable.Subscribe(x =>
                 * {
                 *  Write(x.ToString());
                 * });
                 */

                //await Task.Delay(100);

                //var messages0 = await accept.ReceiveObservable.FirstAsync();
                //var messages1 = await accept.ReceiveObservable.FirstAsync();

                //var messages1 = await accept.ReceiveObservable.ToByteArrayOfLengthPrefix().ToStringArray().FirstAsync();
                //var messages2 = await accept.ReceiveObservable.ToByteArrayOfLengthPrefix().ToStringArray().FirstAsync();

                ;
                //var messages = await accept.ReceiveObservable.ToByteArrayOfLengthPrefix().ToStringArray().Take(2).ToList();
                //var versions = messages[0].Single();

                //if (messages[1][0] != "71") // receive StartApi message
                //    throw new InvalidDataException("StartApi message not received.");

                //new[] { "149", DateTime.Now.ToString("yyyyMMdd HH:mm:ss XXX")}.ToByteArrayWithLengthPrefix().SendTo(accept);
                //new[] { "15", "1", "123,456,789" }.ToByteArrayWithLengthPrefix().SendTo(accept);
                //new[] { "9", "1", "10" }.ToByteArrayWithLengthPrefix().SendTo(accept);
            });

            "A".ToByteArray().SendTo(client);

            // Start sending and receiving messages with an int32 message length prefix (UseV100Plus).
            new[] { $"B" }
            .ToByteArrayWithLengthPrefix().SendTo(client);

            new[] { "C", "D" }
            .ToByteArrayWithLengthPrefix().SendTo(client);

            await Task.Delay(3000);

            ;

            Write("complete");


            client.Dispose();
        }
示例#17
0
        public async Task T08_ExternalDisposeDuringSend()
        {
            var client = await RxSocketClient.ConnectAsync(IPEndPoint, SocketClientLogger);

            var accept   = await AcceptTask;
            var sendTask = Task.Run(() => client.Send(new byte[100_000_000]));