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
コード例 #1
0
ファイル: StatefulMain.cs プロジェクト: rjansen/unity-ws-demo
    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();
    }
コード例 #2
0
ファイル: WebSocket.cs プロジェクト: chsqn/mahjong-4
    public void Close()
    {
        foreach (var completion in _pendingTasks)
        {
            completion.TrySetCanceled();
        }
        _pendingTasks.Clear();

        _socket.CloseAsync();
    }
コード例 #3
0
 void Disconnect()
 {
     if (w == null)
     {
         Debug.Log("[Client] No websocket to disconnect.");
     }
     else
     {
         Debug.Log("[Client] Disconnect.");
         w.CloseAsync();
     }
 }
コード例 #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;
        }
    }
コード例 #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);
        }
コード例 #6
0
ファイル: WebSocket.cs プロジェクト: poup/MST
 /// <summary>
 /// Close websocket client connection
 /// </summary>
 /// <param name="code"></param>
 /// <param name="reason"></param>
 public void Close(string reason = "")
 {
     socket.CloseAsync(CloseStatusCode.Normal, reason);
 }