Skip to content

Commit

Permalink
[Fix] Disconnecting socket client does not send close code (#2714)
Browse files Browse the repository at this point in the history
* fix?

* whoops, debugging stuff
  • Loading branch information
Misha-133 authored Aug 10, 2023
1 parent d5d7378 commit 6bb3777
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,17 @@ internal override async Task DisconnectInternalAsync(Exception ex = null)
return;
ConnectionState = ConnectionState.Disconnecting;

try
{ _connectCancelToken?.Cancel(false); }
catch { }

if (ex is GatewayReconnectException)
await WebSocketClient.DisconnectAsync(4000).ConfigureAwait(false);
else
await WebSocketClient.DisconnectAsync().ConfigureAwait(false);

try
{
_connectCancelToken?.Cancel(false);
}
catch { }

ConnectionState = ConnectionState.Disconnected;
}

Expand Down
18 changes: 13 additions & 5 deletions src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,29 @@ private async Task DisconnectInternalAsync(int closeCode = 1000, bool isDisposin
{
_isDisconnecting = true;

try
{ _disconnectTokenSource.Cancel(false); }
catch { }

if (_client != null)
{
if (!isDisposing)
{
var status = (WebSocketCloseStatus)closeCode;
try
{ await _client.CloseOutputAsync(status, "", new CancellationToken()); }
{
await _client.CloseOutputAsync(status, "", new CancellationToken());
}
catch { }
}

try
{ _client.Dispose(); }
{
_client.Dispose();
}
catch { }

try
{
_disconnectTokenSource.Cancel(false);
}
catch { }

_client = null;
Expand Down

0 comments on commit 6bb3777

Please sign in to comment.