CloseAsync() public method

Closes the WebSocket connection asynchronously, and releases all associated resources.

This method does nothing if the current state of the connection is Closing or Closed.

This method does not wait for the close to be complete.

public CloseAsync ( ) : void
return void
Ejemplo n.º 1
0
    void Start() {
        ws = new WebSocket("ws://echo.websocket.org");

        ws.OnOpen += OnOpenHandler;
        ws.OnMessage += OnMessageHandler;
        ws.OnClose += OnCloseHandler;

        stateMachine.AddHandler(State.Running, () => {
            new Wait(this, 3, () => {
                ws.ConnectAsync();
            });
        });

        stateMachine.AddHandler(State.Connected, () => {
            stateMachine.Transition(State.Ping);
        });

        stateMachine.AddHandler(State.Ping, () => {
            new Wait(this, 3, () => {
               ws.SendAsync("This WebSockets stuff is a breeze!", OnSendComplete);
            });
        });

        stateMachine.AddHandler(State.Pong, () => {
            new Wait(this, 3, () => {
                ws.CloseAsync();
            });
        });

        stateMachine.Run();
    }
Ejemplo n.º 2
0
    public void Close()
    {
        foreach (var completion in _pendingTasks)
        {
            completion.TrySetCanceled();
        }
        _pendingTasks.Clear();

        _socket.CloseAsync();
    }
Ejemplo n.º 3
0
 void Disconnect()
 {
     if (w == null)
     {
         Debug.Log("[Client] No websocket to disconnect.");
     }
     else
     {
         Debug.Log("[Client] Disconnect.");
         w.CloseAsync();
     }
 }
Ejemplo n.º 4
0
    private void unregister()
    {
        if (_socket != null)
        {
            _socket.OnOpen    -= open;
            _socket.OnClose   -= close;
            _socket.OnError   -= error;
            _socket.OnMessage -= message;

            _socket.CloseAsync(WebSocketSharp.CloseStatusCode.Undefined, null);
            _socket = null;
        }
    }
Ejemplo n.º 5
0
        public override Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
        {
            if (m_state != WebSocketState.Open)
            {
                throw new InvalidOperationException();
            }

            var closeTCS = new TaskCompletionSource <object>();

            try
            {
                m_connectCloseTCS = closeTCS;
                m_ws.CloseAsync();
            }
            catch (Exception ex)
            {
                closeTCS.SetException(new WebSocketException(0, ex));
            }

            return(closeTCS.Task);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Close websocket client connection
 /// </summary>
 /// <param name="code"></param>
 /// <param name="reason"></param>
 public void Close(string reason = "")
 {
     socket.CloseAsync(CloseStatusCode.Normal, reason);
 }