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.
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(); }
public void Close() { foreach (var completion in _pendingTasks) { completion.TrySetCanceled(); } _pendingTasks.Clear(); _socket.CloseAsync(); }
void Disconnect() { if (w == null) { Debug.Log("[Client] No websocket to disconnect."); } else { Debug.Log("[Client] Disconnect."); w.CloseAsync(); } }
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; } }
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); }
/// <summary> /// Close websocket client connection /// </summary> /// <param name="code"></param> /// <param name="reason"></param> public void Close(string reason = "") { socket.CloseAsync(CloseStatusCode.Normal, reason); }