public AsyncWebSocketClient(Uri uri, Func<AsyncWebSocketClient, string, Task> onServerTextReceived = null, Func<AsyncWebSocketClient, byte[], int, int, Task> onServerBinaryReceived = null, Func<AsyncWebSocketClient, Task> onServerConnected = null, Func<AsyncWebSocketClient, Task> onServerDisconnected = null, AsyncWebSocketClientConfiguration configuration = null) : this(uri, new InternalAsyncWebSocketClientMessageDispatcherImplementation( onServerTextReceived, onServerBinaryReceived, onServerConnected, onServerDisconnected), configuration) { }
public AsyncWebSocketClient(Uri uri, IAsyncWebSocketClientMessageDispatcher dispatcher, AsyncWebSocketClientConfiguration configuration = null) { if (uri == null) throw new ArgumentNullException("uri"); if (dispatcher == null) throw new ArgumentNullException("dispatcher"); if (!Consts.WebSocketSchemes.Contains(uri.Scheme.ToLowerInvariant())) throw new NotSupportedException( string.Format("Not support the specified scheme [{0}].", uri.Scheme)); _uri = uri; var host = _uri.Host; var port = _uri.Port > 0 ? _uri.Port : uri.Scheme.ToLowerInvariant() == "wss" ? 443 : 80; IPAddress ipAddress; if (IPAddress.TryParse(host, out ipAddress)) { _remoteEndPoint = new IPEndPoint(ipAddress, port); } else { if (host.ToLowerInvariant() == "localhost") { _remoteEndPoint = new IPEndPoint(IPAddress.Parse(@"127.0.0.1"), port); } else { IPAddress[] addresses = Dns.GetHostAddresses(host); if (addresses.Any()) { _remoteEndPoint = new IPEndPoint(addresses.First(), port); } else { throw new InvalidOperationException( string.Format("Cannot resolve host [{0}] by DNS.", host)); } } } _dispatcher = dispatcher; _configuration = configuration ?? new AsyncWebSocketClientConfiguration(); _sslEnabled = uri.Scheme.ToLowerInvariant() == "wss"; Initialize(); }
public AsyncWebSocketClient(Uri uri, IAsyncWebSocketClientMessageDispatcher dispatcher, AsyncWebSocketClientConfiguration configuration = null) { if (uri == null) throw new ArgumentNullException("uri"); if (dispatcher == null) throw new ArgumentNullException("dispatcher"); if (!Consts.WebSocketSchemes.Contains(uri.Scheme.ToLowerInvariant())) throw new NotSupportedException( string.Format("Not support the specified scheme [{0}].", uri.Scheme)); _uri = uri; _remoteEndPoint = ResolveRemoteEndPoint(_uri); _dispatcher = dispatcher; _configuration = configuration ?? new AsyncWebSocketClientConfiguration(); _sslEnabled = uri.Scheme.ToLowerInvariant() == "wss"; Initialize(); }
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)); } } }