Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Disconnecting socket client does not send close code #2714

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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