public async Task ConnectAsync(string url) { Uri uri = new Uri(url); string query = uri.PathAndQuery; bool ssl = uri.Scheme.ToLower().StartsWith("wss"); int port = uri.Port > 0 ? uri.Port : (ssl ? 443 : 80); await client.ConnectAsync(uri.Host, port); ConnectionStream stream = new ConnectionStream(ref client, ssl); if (ssl) { await stream.Authenticate(ConnectionStream.AuthMode.Client, uri.Host); } if (!(await Handshake(stream, uri, port))) { state = WebsockState.Closed; throw new Exception("Failed connection at handshake"); } protocol = new WebsockProtocol(ref stream); state = WebsockState.Open; try { while (true) { await OnMessage(await protocol.GetMessage()); } } catch (WebsockCloseException ex) { await OnClose(ex.Code, ex.Reason); } finally { protocol.Dispose(); } }
public async Task StartAsync() { // handle incoming data try { while (true) { await OnMessage(await protocol.GetMessage()); } // handle websock closing } catch (WebsockCloseException ex) { await OnClose(ex.Code, ex.Reason); // close client and destroy stream on exit } finally { if (server.GetClients().Contains(this)) { server.GetClients().Remove(this); } protocol.Dispose(); } }