Exemple #1
0
        public async Task Test()
        {
            InterceptData clientData = null, serverData = null;
            // create the server
            var server = new WsServer();
            await server.StartAsync(new IPEndPoint(IPAddress.Any, 8080), async stream =>
            {
                // measure server data
                await stream.WrapSocketAsync(x =>
                                             // create a new interceptor
                                             Task.FromResult <WStreamSocket>(new Interceptor(x, out serverData))
                                             );
                // called when a client connects
                await stream.WriteAsync(BitConverter.GetBytes(DateTime.Now.ToBinary()));
                await stream.CloseAsync();
            });

            // start client
            var client = new WsClient();
            var connection = await client.ConnectAsync(new Uri("ws://localhost:8080"));

            // measure client data
            await connection.WrapSocketAsync(x =>
                                             // create a new interceptor
                                             Task.FromResult <WStreamSocket>(new Interceptor(x, out clientData))
                                             );

            // read data
            var binReader = new BinaryReader(connection);

            Console.WriteLine($"Current time is {DateTime.FromBinary(binReader.ReadInt64())}");

            Console.WriteLine($"Intercepted Data from client - Written {clientData.BytesWritten} / Read {clientData.BytesRead}");
            Console.WriteLine($"Intercepted Data from server - Written {serverData.BytesWritten} / Read {serverData.BytesRead}");

            // cleanup
            client.Dispose();
            server.Dispose();
        }
Exemple #2
0
 public Interceptor(WStreamSocket socket, out InterceptData data) : base(socket)
 {
     _data = new InterceptData();
     data  = _data;
 }