public static void WebSocketClientTest(string[] args) { IPEndPoint serverEP = new IPEndPoint(IPAddress.Any, 420); IPEndPoint clientEP = new IPEndPoint(IPAddress.Loopback, 420); WebSocketListener listener = new WebSocketListener(serverEP); WebSocket server = null; WebSocket client = null; listener.ClientVerifier = (address, subprotocols, head) => subprotocols.Contains("test"); listener.SubprotocolChooser = (address, subprotocols, head) => "test"; listener.OnConnection += (connection) => { server = connection; Console.WriteLine("listener connection"); Console.WriteLine(server.Subprotocol); server.OnBinary += (data) => Console.WriteLine("SERVER BINARY {0}", data.LongLength); server.OnString += (data) => Console.WriteLine("SERVER STRING {0}", data.Length); server.OnClose += (code, reason) => Console.WriteLine("SERVER CLOSED {0} '{1}'", code, reason); server.SendBinary(new byte[1]); server.SendBinary(new byte[10]); server.SendBinary(new byte[100]); server.SendString("Hijklmn"); server.SendClose(1000, "OK"); }; listener.Start(); Console.WriteLine("listener open"); Connection clientTcp = new Connection(); clientTcp.OnOpen += () => { client = WebSocketFactory.Default.Generate(clientTcp, "/"); client.OnOpen += () => { client.OnBinary += (data) => Console.WriteLine("CLIENT BINARY {0}", data.LongLength); client.OnString += (data) => Console.WriteLine("CLIENT STRING {0}", data.Length); client.OnClose += (code, reason) => Console.WriteLine("CLIENT CLOSED {0} '{1}'", code, reason); /*client.SendBinary(new byte[1]); * client.SendBinary(new byte[10]); * client.SendBinary(new byte[100]); * client.SendString("Abcdefg");*/ client.SendClose(1000, "OK"); }; }; clientTcp.Connect(clientEP); Console.ReadKey(); Console.WriteLine("client states: {0} {1} {2} {3}", client.Opening, client.Open, client.Closing, client.Closed); Console.ReadKey(); listener.Stop(); Console.ReadKey(); }
public static void WebSocketServerTest(string[] args) { IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 420); WebSocketListener server = new WebSocketListener(endPoint); WebSocket client = null; server.OnConnection += (connection) => { client = connection; Console.WriteLine("connection"); connection.OnBinary += (data) => Console.WriteLine("BINARY {0}", data.LongLength); connection.OnString += (data) => Console.WriteLine("STRING {0}", data.Length); connection.OnClose += (code, reason) => Console.WriteLine("CLOSED {0} '{1}'", code, reason); connection.SendBinary(new byte[1]); connection.SendBinary(new byte[10]); connection.SendBinary(new byte[100]); }; Console.WriteLine("open"); server.Start(); Console.ReadKey(); client.SendClose(1000, "OK"); server.Stop(); Console.ReadKey(); }