Skip to content

Commit

Permalink
impl WebSocketClient.cs Open
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Nov 4, 2023
1 parent 7a36404 commit 93b4325
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/WebSocket/WebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,53 @@ public WebSocketClient()

public void Open(Uri uri)
{
if (IsOpened || _tryConnecting || _tryClosing) return;

_tryConnecting = true;

ThreadPool.QueueUserWorkItem(SubTask);

async void SubTask(object _)
{
try
{
var ws = new ClientWebSocket();
_onModify?.Invoke(null, ws);
await ws.ConnectAsync(uri, CancellationToken);

_websocket = ws;
Uri = uri;

// TODO: IMP -> CACHING COOKIES (REQUIRE REFLECTIONS)
// TODO: IMP -> CACHING HEADERS (REQUIRE REFLECTIONS)

_onOpen?.Invoke(null, null);

_ReceiveData();
}
catch (Exception e)
{
try
{
await _websocket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken);
}
catch
{
// ignored
}
finally
{
_websocket = null;
}

_onError?.Invoke(null, e);
}
finally
{
_tryClosing = false;
_tryConnecting = false;
}
}
}


Expand Down

0 comments on commit 93b4325

Please sign in to comment.