Esempio n. 1
0
        static void Main(string[] args)
        {
            try
            {
                var config = new WebSocketClientConfiguration();
                //config.SslTargetHost = "Cowboy";
                //config.SslClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(@"D:\\Cowboy.cer"));
                //config.SslPolicyErrorsBypassed = true;

                //var uri = new Uri("ws://echo.websocket.org/");
                //var uri = new Uri("wss://127.0.0.1:22222/test");
                var uri = new Uri("ws://127.0.0.1:22222/test");
                _client = new WebSocketClient(uri, config, _log);
                _client.ServerConnected      += OnServerConnected;
                _client.ServerDisconnected   += OnServerDisconnected;
                _client.ServerTextReceived   += OnServerTextReceived;
                _client.ServerBinaryReceived += OnServerBinaryReceived;
                _client.Connect();

                Console.WriteLine("WebSocket client has connected to server [{0}].", uri);
                Console.WriteLine("Type something to send to server...");
                while (_client.State == WebSocketState.Open)
                {
                    try
                    {
                        string text = Console.ReadLine();
                        if (text == "quit")
                        {
                            break;
                        }

                        if (text == "many")
                        {
                            text = new string('x', 1024);
                            Stopwatch watch = Stopwatch.StartNew();
                            int       count = 10000;
                            for (int i = 1; i <= count; i++)
                            {
                                _client.BeginSendBinary(Encoding.UTF8.GetBytes(text));
                                Console.WriteLine("Client [{0}] send binary -> Sequence[{1}] -> TextLength[{2}].",
                                                  _client.LocalEndPoint, i, text.Length);
                            }
                            watch.Stop();
                            Console.WriteLine("Client [{0}] send binary -> Count[{1}] -> Cost[{2}] -> PerSecond[{3}].",
                                              _client.LocalEndPoint, count, watch.ElapsedMilliseconds / 1000, count / (watch.ElapsedMilliseconds / 1000));
                        }
                        else if (text == "big1")
                        {
                            text = new string('x', 1024 * 1024 * 1);
                            _client.BeginSendBinary(Encoding.UTF8.GetBytes(text));
                            Console.WriteLine("Client [{0}] send binary -> [{1} Bytes].", _client.LocalEndPoint, text.Length);
                        }
                        else if (text == "big10")
                        {
                            text = new string('x', 1024 * 1024 * 10);
                            _client.BeginSendBinary(Encoding.UTF8.GetBytes(text));
                            Console.WriteLine("Client [{0}] send binary -> [{1} Bytes].", _client.LocalEndPoint, text.Length);
                        }
                        else if (text == "big100")
                        {
                            text = new string('x', 1024 * 1024 * 100);
                            _client.BeginSendBinary(Encoding.UTF8.GetBytes(text));
                            Console.WriteLine("Client [{0}] send binary -> [{1} Bytes].", _client.LocalEndPoint, text.Length);
                        }
                        else if (text == "big1000")
                        {
                            text = new string('x', 1024 * 1024 * 1000);
                            _client.BeginSendBinary(Encoding.UTF8.GetBytes(text));
                            Console.WriteLine("Client [{0}] send binary -> [{1} Bytes].", _client.LocalEndPoint, text.Length);
                        }
                        else
                        {
                            _client.BeginSendBinary(Encoding.UTF8.GetBytes(text));
                            Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text);

                            //_client.SendText(text);
                            //Console.WriteLine("Client [{0}] send text -> [{1}].", _client.LocalEndPoint, text);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

                _client.Close(WebSocketCloseCode.NormalClosure);
                Console.WriteLine("WebSocket client has disconnected from server [{0}].", uri);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }