protected virtual async Task ListenForConnectionsAsync() { while (_isRunning) { try { var client = await _server.AcceptTcpClientAsync(); var stream = client.GetStream(); var connection = new ConnectionWSServer { Websocket = WebSocket.CreateFromStream(stream, true, null, WebSocket.DefaultKeepAliveInterval), ConnectionId = Guid.NewGuid().ToString(), Stream = stream, Client = client }; _ = Task.Run(async() => { await StartReceivingMessagesAsync(connection); }); } catch (Exception ex) { await FireEventAsync(this, new WSErrorServerEventArgs { Exception = ex, Message = ex.Message, }); } } }
protected virtual async Task ListenForConnectionsSSLAsync() { while (_isRunning) { try { var client = await _server.AcceptTcpClientAsync(); var sslStream = new SslStream(client.GetStream()); await sslStream.AuthenticateAsServerAsync(new X509Certificate2(_certificate, _certificatePassword)); if (sslStream.IsAuthenticated && sslStream.IsEncrypted) { var connection = new ConnectionWSServer { Websocket = WebSocket.CreateFromStream(sslStream, true, null, WebSocket.DefaultKeepAliveInterval), ConnectionId = Guid.NewGuid().ToString(), Client = client, Stream = sslStream }; _ = Task.Run(async() => { await StartReceivingMessagesAsync(connection); }); } else { var certStatus = $"IsAuthenticated = {sslStream.IsAuthenticated} && IsEncripted == {sslStream.IsEncrypted}"; await FireEventAsync(this, new WSErrorServerEventArgs { Exception = new Exception(certStatus), Message = certStatus }); client.Close(); } } catch (Exception ex) { await FireEventAsync(this, new WSErrorServerEventArgs { Exception = ex, Message = ex.Message, }); } } }