|
@@ -16,7 +16,7 @@ namespace Discord.Net.WebSockets |
|
|
private readonly ConcurrentQueue<string> _sendQueue; |
|
|
private readonly ConcurrentQueue<string> _sendQueue; |
|
|
private readonly TaskManager _taskManager; |
|
|
private readonly TaskManager _taskManager; |
|
|
private WebSocketClient _webSocket; |
|
|
private WebSocketClient _webSocket; |
|
|
private ManualResetEventSlim _waitUntilConnect; |
|
|
|
|
|
|
|
|
private ManualResetEventSlim _waitUntilConnect, _waitUntilDisconnect; |
|
|
|
|
|
|
|
|
public event EventHandler<WebSocketBinaryMessageEventArgs> BinaryMessage = delegate { }; |
|
|
public event EventHandler<WebSocketBinaryMessageEventArgs> BinaryMessage = delegate { }; |
|
|
public event EventHandler<WebSocketTextMessageEventArgs> TextMessage = delegate { }; |
|
|
public event EventHandler<WebSocketTextMessageEventArgs> TextMessage = delegate { }; |
|
@@ -31,6 +31,7 @@ namespace Discord.Net.WebSockets |
|
|
_taskManager = taskManager; |
|
|
_taskManager = taskManager; |
|
|
_sendQueue = new ConcurrentQueue<string>(); |
|
|
_sendQueue = new ConcurrentQueue<string>(); |
|
|
_waitUntilConnect = new ManualResetEventSlim(); |
|
|
_waitUntilConnect = new ManualResetEventSlim(); |
|
|
|
|
|
_waitUntilDisconnect = new ManualResetEventSlim(true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Task Connect(string host, CancellationToken cancelToken) |
|
|
public Task Connect(string host, CancellationToken cancelToken) |
|
@@ -62,13 +63,15 @@ namespace Discord.Net.WebSockets |
|
|
_webSocket = null; |
|
|
_webSocket = null; |
|
|
if (socket != null) |
|
|
if (socket != null) |
|
|
{ |
|
|
{ |
|
|
//We dont want a slow disconnect to mess up the next connection, so lets just unregister events |
|
|
|
|
|
|
|
|
socket.Close(); |
|
|
|
|
|
socket.Opened -= OnWebSocketOpened; |
|
|
socket.DataReceived -= OnWebSocketBinary; |
|
|
socket.DataReceived -= OnWebSocketBinary; |
|
|
socket.MessageReceived -= OnWebSocketText; |
|
|
socket.MessageReceived -= OnWebSocketText; |
|
|
|
|
|
|
|
|
|
|
|
_waitUntilDisconnect.Wait(); //We need the next two events to raise this one |
|
|
socket.Error -= OnWebSocketError; |
|
|
socket.Error -= OnWebSocketError; |
|
|
socket.Closed -= OnWebSocketClosed; |
|
|
socket.Closed -= OnWebSocketClosed; |
|
|
socket.Opened -= OnWebSocketOpened; |
|
|
|
|
|
socket.Close(); |
|
|
|
|
|
|
|
|
socket.Dispose(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return TaskHelper.CompletedTask; |
|
|
return TaskHelper.CompletedTask; |
|
@@ -78,6 +81,7 @@ namespace Discord.Net.WebSockets |
|
|
{ |
|
|
{ |
|
|
_taskManager.SignalError(e.Exception); |
|
|
_taskManager.SignalError(e.Exception); |
|
|
_waitUntilConnect.Set(); |
|
|
_waitUntilConnect.Set(); |
|
|
|
|
|
_waitUntilDisconnect.Set(); |
|
|
} |
|
|
} |
|
|
private void OnWebSocketClosed(object sender, EventArgs e) |
|
|
private void OnWebSocketClosed(object sender, EventArgs e) |
|
|
{ |
|
|
{ |
|
@@ -85,12 +89,16 @@ namespace Discord.Net.WebSockets |
|
|
if (e is ClosedEventArgs) |
|
|
if (e is ClosedEventArgs) |
|
|
ex = new WebSocketException((e as ClosedEventArgs).Code, (e as ClosedEventArgs).Reason); |
|
|
ex = new WebSocketException((e as ClosedEventArgs).Code, (e as ClosedEventArgs).Reason); |
|
|
else |
|
|
else |
|
|
ex = new Exception($"Connection lost"); |
|
|
|
|
|
|
|
|
ex = new Exception("Connection lost"); |
|
|
_taskManager.SignalError(ex); |
|
|
_taskManager.SignalError(ex); |
|
|
_waitUntilConnect.Set(); |
|
|
_waitUntilConnect.Set(); |
|
|
|
|
|
_waitUntilDisconnect.Set(); |
|
|
} |
|
|
} |
|
|
private void OnWebSocketOpened(object sender, EventArgs e) |
|
|
private void OnWebSocketOpened(object sender, EventArgs e) |
|
|
=> _waitUntilConnect.Set(); |
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
_waitUntilDisconnect.Reset(); |
|
|
|
|
|
_waitUntilConnect.Set(); |
|
|
|
|
|
} |
|
|
private void OnWebSocketText(object sender, MessageReceivedEventArgs e) |
|
|
private void OnWebSocketText(object sender, MessageReceivedEventArgs e) |
|
|
=> OnTextMessage(e.Message); |
|
|
=> OnTextMessage(e.Message); |
|
|
private void OnWebSocketBinary(object sender, DataReceivedEventArgs e) |
|
|
private void OnWebSocketBinary(object sender, DataReceivedEventArgs e) |
|
|