public async Task CloseConnection(WebSocketCloseStatus status = WebSocketCloseStatus.NormalClosure, string desc = null)
        {
            try
            {
                Global.ExtensionsManager.BeforeConnectionClose(this);
                if (webSocket.State == WebSocketState.Open)
                {
                    var connectionPubKey = "no public key";
                    if (ClientPubKey != null)
                    {
                        connectionPubKey = $"public key {ClientPubKey}";
                    }
                    desc = desc ?? status.ToString();
                    logger.Trace($"Connection with {connectionPubKey} is closed. Status: {status}, description: {desc}");

                    await sendMessageSemaphore.WaitAsync();

                    try
                    {
                        var timeoutTokenSource = new CancellationTokenSource(1000);
                        await webSocket.CloseAsync(status, desc, timeoutTokenSource.Token);

                        cancellationTokenSource?.Cancel();
                    }
                    catch (WebSocketException exc)
                    {
                        //ignore client disconnection
                        if (exc.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely &&
                            exc.WebSocketErrorCode != WebSocketError.InvalidState)
                        {
                            throw;
                        }
                    }
                    catch (OperationCanceledException) { }
                    finally
                    {
                        sendMessageSemaphore?.Release();
                    }
                }
            }
            catch (Exception e)
            {
                logger.Error(e, "Error on close connection");
            }
        }
예제 #2
0
        public async Task CloseAsync(WebSocketCloseStatus closeState = WebSocketCloseStatus.NormalClosure)
        {
            OnDebug("Closing");
            OnClosing();

            var closeMessage = closeState == WebSocketCloseStatus.NormalClosure
                ? null
                : closeState.ToString();

            await Socket
            .CloseAsync(closeState, closeMessage, CancellationToken.None)
            .ConfigureAwait(false);

            while (State == WebSocketState.Open ||
                   State == WebSocketState.CloseSent)
            {
                await Task.Yield();
            }
        }
예제 #3
0
 private void ChatClient_DisconnectOccurred(object sender, WebSocketCloseStatus e)
 {
     Assert.Fail("Disconnection occurred: " + e.ToString());
 }
 public override void OnClose(WebSocketCloseStatus? closeStatus, string closeStatusDescription)
 {
     logger.Info("ContainerProcessHandler.OnClose", new Dictionary<string, object> {{"closeStatus", closeStatus.ToString()}, {"closeStatusDescription", closeStatusDescription}});
 }
 public override void OnClose(WebSocketCloseStatus? closeStatus, string closeStatusDescription)
 {
     logger.Info("OnClose: {0} :: {1}", closeStatus.ToString(), closeStatusDescription);
 }
 private void WSOnClosed(WebSocketCloseStatus reason)
 {
     logger.LogInformation($"OnClosed - Connection Closed: {reason.ToString()}");
 }
 /// <summary>
 /// Stopping connection to a websocket
 /// </summary>
 public async Task StopAsync(WebSocketCloseStatus status, string statusDescription)
 {
     //TODO Finish implementation of the stop method for the socketclient
     this._logger?.Debug($"Connection will be closed with this status {status.ToString()} and the following description: {statusDescription}");
     await this._webSocket.CloseAsync(status, statusDescription, this._cancellationTokenSource.Token);
 }
예제 #8
0
 private void WS_OnClosed(WebSocketCloseStatus reason)
 {
     Console.WriteLine($"WS Socket closed => {reason.ToString()}");
 }