Skip to content

Commit 4f48d4d

Browse files
committed
Protocol negotiation closes #5
1 parent 65170cb commit 4f48d4d

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

AngleSharp.Io/Dom/WebSocket.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,28 @@ public WebSocket(IWindow window, String url, params String[] protocols)
9898
throw new DomException(DomError.Syntax);
9999

100100
_ws = new ClientWebSocket();
101+
102+
foreach (var protocol in protocols)
103+
_ws.Options.AddSubProtocol(protocol);
104+
101105
_ws.Options.KeepAliveInterval = TimeSpan.FromSeconds(20);
102106
ConnectAsync(url).Forget();
103107
}
104108

105109
async Task ConnectAsync(String url)
106110
{
107-
await _ws.ConnectAsync(new Uri(url), _cts.Token).ConfigureAwait(false);
108-
_state = WebSocketReadyState.Open;
109-
OnConnected();
110-
ListenAsync().Forget();
111+
try
112+
{
113+
await _ws.ConnectAsync(new Uri(url), _cts.Token).ConfigureAwait(false);
114+
_state = WebSocketReadyState.Open;
115+
OnConnected();
116+
ListenAsync().Forget();
117+
}
118+
catch (Exception ex)
119+
{
120+
_state = WebSocketReadyState.Closed;
121+
OnError(ex);
122+
}
111123
}
112124

113125
#endregion
@@ -249,7 +261,10 @@ async Task ListenAsync()
249261
var result = await _ws.ReceiveAsync(segment, _cts.Token).ConfigureAwait(false);
250262

251263
if (result.MessageType == WebSocketMessageType.Close)
264+
{
265+
await CloseAsync().ConfigureAwait(false);
252266
break;
267+
}
253268

254269
stringResult.Append(Encoding.UTF8.GetString(buffer, 0, result.Count));
255270

@@ -259,8 +274,6 @@ async Task ListenAsync()
259274
stringResult.Clear();
260275
}
261276
}
262-
263-
await CloseAsync().ConfigureAwait(false);
264277
}
265278
catch (Exception ex)
266279
{

0 commit comments

Comments
 (0)