static void Main(string[] args) { NLogLogger.Use(); Task.Run(async() => { try { var config = new AsyncWebSocketClientConfiguration(); //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 AsyncWebSocketClient(uri, OnServerTextReceived, OnServerBinaryReceived, OnServerConnected, OnServerDisconnected, config); await _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; } Task.Run(async() => { //await _client.SendText(text); //Console.WriteLine("Client [{0}] send text -> [{1}].", _client.LocalEndPoint, text); await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); }).Forget(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } await _client.Close(WebSocketCloseCode.NormalClosure); Console.WriteLine("WebSocket client has disconnected from server [{0}].", uri); } catch (Exception ex) { Logger.Get <Program>().Error(ex.Message, ex); } }).Wait(); Console.ReadKey(); }
static void Main(string[] args) { NLogLogger.Use(); Task.Run(async () => { try { var config = new AsyncWebSocketClientConfiguration(); //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 AsyncWebSocketClient(uri, OnServerTextReceived, OnServerBinaryReceived, OnServerConnected, OnServerDisconnected, config); await _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; Task.Run(async () => { //await _client.SendText(text); //Console.WriteLine("Client [{0}] send text -> [{1}].", _client.LocalEndPoint, text); await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); }).Forget(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } await _client.Close(WebSocketCloseCode.NormalClosure); Console.WriteLine("WebSocket client has disconnected from server [{0}].", uri); } catch (Exception ex) { Logger.Get<Program>().Error(ex.Message, ex); } }).Wait(); Console.ReadKey(); }
static void Main(string[] args) { NLogLogger.Use(); Task.Run(async () => { try { var config = new AsyncWebSocketClientConfiguration(); //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 AsyncWebSocketClient(uri, OnServerTextReceived, OnServerBinaryReceived, OnServerConnected, OnServerDisconnected, config); await _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; Task.Run(async () => { if (text == "many") { text = new string('x', 1024); Stopwatch watch = Stopwatch.StartNew(); int count = 10000; for (int i = 1; i <= count; i++) { await _client.SendBinaryAsync(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 == "big") { text = new string('x', 1024 * 1024 * 100); await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); } else if (text == "8192") { text = new string('x', 1024 * 8); await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); } else if (text == "4096") { text = new string('x', 1024 * 4); await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); } else if (text == "2048") { text = new string('x', 1024 * 2); await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); } else if (text == "1024") { text = new string('x', 1024); await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); } else { await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); //await _client.SendTextAsync(text); //Console.WriteLine("Client [{0}] send text -> [{1}].", _client.LocalEndPoint, text); } }).Forget(); } catch (Exception ex) { Logger.Get<Program>().Error(ex.Message, ex); } } await _client.Close(WebSocketCloseCode.NormalClosure); Console.WriteLine("WebSocket client has disconnected from server [{0}].", uri); } catch (Exception ex) { Logger.Get<Program>().Error(ex.Message, ex); } }).Wait(); Console.ReadKey(); }
private async Task PerformWebSocketLoad(int connections, IPEndPoint remoteEP) { var channels = new List <AsyncWebSocketClient>(); var configuration = new AsyncWebSocketClientConfiguration(); configuration.SslPolicyErrorsBypassed = _options.IsSetSslPolicyErrorsBypassed; configuration.SslTargetHost = _options.SslTargetHost; configuration.SslClientCertificates = _options.SslClientCertificates; string uriString = string.Format("{0}://{1}/{2}", _options.IsSetSsl ? "wss" : "ws", remoteEP, _options.IsSetWebSocketPath ? _options.WebSocketPath.TrimStart('/') : ""); var uri = new Uri(uriString); for (int c = 0; c < connections; c++) { var client = new AsyncWebSocketClient(uri, onServerTextReceived: async(s, b) => { await Task.CompletedTask; }, onServerBinaryReceived: async(s, b, o, l) => { await Task.CompletedTask; }, onServerConnected: async(s) => { await Task.CompletedTask; }, onServerDisconnected: async(s) => { await Task.CompletedTask; }, configuration: configuration); try { _logger(string.Format("Connecting to [{0}].", remoteEP)); await client.Connect(); channels.Add(client); _logger(string.Format("Connected to [{0}] from [{1}].", remoteEP, client.LocalEndPoint)); } catch (Exception ex) when(!ShouldThrow(ex)) { if (ex is AggregateException) { var a = ex as AggregateException; if (a.InnerExceptions != null && a.InnerExceptions.Any()) { _logger(string.Format("Connect to [{0}] error occurred [{1}].", remoteEP, a.InnerExceptions.First().Message)); } } else { _logger(string.Format("Connect to [{0}] error occurred [{1}].", remoteEP, ex.Message)); } if (client != null) { await client.Close(WebSocketCloseCode.AbnormalClosure); } } } if (_options.IsSetChannelLifetime && channels.Any()) { await Task.Delay(_options.ChannelLifetime); } foreach (var client in channels) { try { _logger(string.Format("Closed to [{0}] from [{1}].", remoteEP, client.LocalEndPoint)); await client.Close(WebSocketCloseCode.NormalClosure); } catch (Exception ex) when(!ShouldThrow(ex)) { if (ex is AggregateException) { var a = ex as AggregateException; if (a.InnerExceptions != null && a.InnerExceptions.Any()) { _logger(string.Format("Closed to [{0}] error occurred [{1}].", remoteEP, a.InnerExceptions.First().Message)); } } else { _logger(string.Format("Closed to [{0}] error occurred [{1}].", remoteEP, ex.Message)); } } } }
static void Main(string[] args) { NLogLogger.Use(); Task.Run(async() => { try { var config = new AsyncWebSocketClientConfiguration(); //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 AsyncWebSocketClient(uri, OnServerTextReceived, OnServerBinaryReceived, OnServerConnected, OnServerDisconnected, config); await _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; } Task.Run(async() => { if (text == "many") { text = new string('x', 1024); Stopwatch watch = Stopwatch.StartNew(); int count = 10000; for (int i = 1; i <= count; i++) { await _client.SendBinaryAsync(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 == "big") { text = new string('x', 1024 * 1024 * 100); await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); } else { await _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Client [{0}] send binary -> [{1}].", _client.LocalEndPoint, text); } //await _client.SendText(text); //Console.WriteLine("Client [{0}] send text -> [{1}].", _client.LocalEndPoint, text); }).Forget(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } await _client.Close(WebSocketCloseCode.NormalClosure); Console.WriteLine("WebSocket client has disconnected from server [{0}].", uri); } catch (Exception ex) { Logger.Get <Program>().Error(ex.Message, ex); } }).Wait(); Console.ReadKey(); }