private void notifyClose(int code, String reason) { try { long currentState = Interlocked.Read(ref state); if (currentState == CLOSING) { if (Interlocked.CompareExchange(ref state, CLOSED, currentState) == currentState) { disconnect(); listener.OnClose(1000, ""); } } else if (currentState == OPEN) { if (Interlocked.CompareExchange(ref state, CLOSED, currentState) == currentState) { disconnect(code); listener.OnClose(code, reason); } } } catch (Exception) { // discard exceptions thrown by the listener } return; }
private async Task ConnectToSocket(Uri serverUri) { try { _webSocket = new ClientWebSocket(); await _webSocket.ConnectAsync(serverUri, CancellationToken.None); _webSocketListener.Open(this); await StartListen(); _webSocketListener.OnClose(); } catch (Exception ex) { Console.WriteLine("Exception: {0}", ex); } finally { _webSocket?.Dispose(); Console.WriteLine(); } }