[apibrk] change: Specify WebSocket close code (#1500)

* API breaking change: Specify WebSocket close code

Should fix #1479 and help overall with resuming sessions.

* Also try to resume on missed heartbeats
This commit is contained in:
Monica S
2020-04-25 12:12:57 +01:00
committed by GitHub
parent 6d8e216545
commit ed869bd78b
7 changed files with 48 additions and 31 deletions

View File

@@ -44,7 +44,7 @@ namespace Discord.Net.WebSockets
{
if (disposing)
{
DisconnectInternalAsync(true).GetAwaiter().GetResult();
DisconnectInternalAsync(isDisposing: true).GetAwaiter().GetResult();
_disconnectTokenSource?.Dispose();
_cancelTokenSource?.Dispose();
_lock?.Dispose();
@@ -94,19 +94,19 @@ namespace Discord.Net.WebSockets
_task = RunAsync(_cancelToken);
}
public async Task DisconnectAsync()
public async Task DisconnectAsync(int closeCode = 1000)
{
await _lock.WaitAsync().ConfigureAwait(false);
try
{
await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(closeCode: closeCode).ConfigureAwait(false);
}
finally
{
_lock.Release();
}
}
private async Task DisconnectInternalAsync(bool isDisposing = false)
private async Task DisconnectInternalAsync(int closeCode = 1000, bool isDisposing = false)
{
try { _disconnectTokenSource.Cancel(false); }
catch { }
@@ -117,7 +117,8 @@ namespace Discord.Net.WebSockets
{
if (!isDisposing)
{
try { await _client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", new CancellationToken()); }
var status = (WebSocketCloseStatus)closeCode;
try { await _client.CloseOutputAsync(status, "", new CancellationToken()); }
catch { }
}
try { _client.Dispose(); }
@@ -141,7 +142,7 @@ namespace Discord.Net.WebSockets
await _lock.WaitAsync().ConfigureAwait(false);
try
{
await DisconnectInternalAsync(false);
await DisconnectInternalAsync(isDisposing: false);
}
finally
{