public HackClient(string url = "wss://hack.chat/chat-ws") { Uri uri = new Uri(url); ws = new WebSocket(uri.ToString()); ws.Log.Output = (data, str) => { // ... }; ws.OnOpen += this.ws_OnOpen; ws.OnClose += this.ws_OnClose; ws.OnMessage += this.ws_OnMessage; ws.OnError += this.ws_OnError; ClientSslConfiguration sslconf = new ClientSslConfiguration(uri.Host); sslconf.EnabledSslProtocols = SslProtocols.Tls13 | SslProtocols.Tls12 | SslProtocols.Tls11; sslconf.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { if (this.ServerCertificateValidationCallback != null) { return(this.ServerCertificateValidationCallback(sender, certificate, chain, sslPolicyErrors)); } return(true); }; ws.SslConfiguration = sslconf; }
public async Task WhenClientSendsTextMessageThenResponds() { const string Message = "Message"; var waitHandle = new ManualResetEventSlim(false); var clientSslConfiguration = new ClientSslConfiguration("localhost", certificateValidationCallback: (sender, certificate, chain, errors) => true); Func <MessageEventArgs, Task> onMessage = e => { if (e.Text.ReadToEnd() == Message) { waitHandle.Set(); } return(AsyncEx.Completed()); }; using (var client = new WebSocket("wss://localhost:443/echo", sslAuthConfiguration: clientSslConfiguration, onMessage: onMessage)) { await client.Connect().ConfigureAwait(false); await client.Send(Message).ConfigureAwait(false); var result = waitHandle.Wait(Debugger.IsAttached ? 30000 : 2000); Assert.True(result); } }
public async Task WhenStreamVeryLargeStreamToServerThenBroadcasts() { var responseLength = 0; const int Length = 1000000; var stream = new EnumerableStream(Enumerable.Repeat((byte)123, Length)); var waitHandle = new ManualResetEventSlim(false); var clientSslConfiguration = new ClientSslConfiguration("localhost", certificateValidationCallback: (sender, certificate, chain, errors) => true); Func <MessageEventArgs, Task> onMessage = async e => { var bytes = await e.Data.ReadBytes(Length).ConfigureAwait(false); responseLength = bytes.Count(x => x == 123); waitHandle.Set(); }; using (var sender = new WebSocket("wss://localhost:443/radio", sslAuthConfiguration: clientSslConfiguration)) { using (var client = new WebSocket("wss://localhost:443/radio", sslAuthConfiguration: clientSslConfiguration, onMessage: onMessage)) { await sender.Connect().ConfigureAwait(false); await client.Connect().ConfigureAwait(false); await sender.Send(stream).ConfigureAwait(false); waitHandle.Wait(); Assert.AreEqual(Length, responseLength); } } }
public async Task WhenClientSendsMultipleTextMessageThenResponds([Random(1, 100, 10)] int multiplicity) { int count = 0; const string Message = "Message"; var waitHandle = new ManualResetEventSlim(false); var clientSslConfiguration = new ClientSslConfiguration("localhost", certificateValidationCallback: (sender, certificate, chain, errors) => true); Func <MessageEventArgs, Task> onMessage = async e => { if (await e.Text.ReadToEndAsync().ConfigureAwait(false) == Message) { if (Interlocked.Increment(ref count) == multiplicity) { waitHandle.Set(); } } }; using (var client = new WebSocket("wss://localhost:443/echo", sslAuthConfiguration: clientSslConfiguration, onMessage: onMessage)) { await client.Connect().ConfigureAwait(false); for (int i = 0; i < multiplicity; i++) { await client.Send(Message).ConfigureAwait(false); } var result = waitHandle.Wait(Debugger.IsAttached ? 30000 : 5000); Assert.True(result); } }
public MazeSocketConnector(Uri serverUri) { _serverUri = serverUri; TcpClient = new TcpClient(); SslConfig = new ClientSslConfiguration(serverUri.DnsSafeHost); _base64Key = CreateBase64Key(); }
public async Task ClientCanConnectToServer() { var clientSslConfiguration = new ClientSslConfiguration( "localhost", clientCertificates: new X509Certificate2Collection(GetRandomCertificate()), enabledSslProtocols: SslProtocols.Tls, certificateValidationCallback: (sender, certificate, chain, errors) => true); using (var client = new WebSocket("wss://localhost:443/echo", sslAuthConfiguration: clientSslConfiguration)) { await client.Connect().ConfigureAwait(false); Assert.AreEqual(WebSocketState.Open, client.ReadyState); } }
public IRawSocketTransportSyntax SslConfiguration(ClientSslConfiguration sslConfiguration) { ((RawSocketActivator)State.ConnectionActivator).SslConfiguration = sslConfiguration; return(this); }