/// <summary> /// Closes the websocket. /// </summary> /// <returns>The async.</returns> public async Task CloseAsync(CancellationToken cancellationToken = default) { if (webSocket.State == WebSocketState.Open) { await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Connection closed by consumer", cancellationToken); } }
protected void DisconnectWebSocket() { if (_ws != null && isAttached) { Debug.Log("Disconnect Web Socket"); _ws.CloseAsync(); } }
public async override Task IsClosingAsync() { if (_webSocket != null) { await _webSocket.CloseAsync(); Debug.WriteLine("Web Socket is Closed."); _webSocket = null; } }
public async Task CloseAsync() { // TODO : We have to provide a description and close status here. await _webSocket.CloseAsync( "closed", CancellationToken.None) .ConfigureAwait(false); Dispose(); }
internal static IWebSocket CreateWebSocketThatConnectsAndClosesSuccessful() { IWebSocket webSocket = CreateWebSocketThatConnectsSuccessful(); A.CallTo(() => webSocket.CloseAsync(CloseStatusCode.Away)).Invokes(() => { webSocket.OnClose += Raise.With(webSocket, EventArgs.Empty as CloseEventArgs); }); return(webSocket); }
async Task IWebSocketConnection.HandleMessagesAsync(IWebSocket socket, CancellationToken cancellation) { WebSocket = socket; _connectionManager.AddSession(this); await OnConnected(); bool onDisconnectedInvoked = false; while (WebSocket.WebSocketState == JsonRpcWebSocketState.Open) { var(type, buffer) = await WebSocket.ReceiveAsync(cancellation); string message = null; if (buffer.Array == null) { _connectionManager.RemoveSession(this); const int invalidPayloadData = 1007; await WebSocket.CloseAsync(invalidPayloadData, "Received empty data buffer"); throw new InvalidOperationException("Received empty data buffer from underlying socket"); } if (type != MessageType.Binary) { message = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count); } if (type == MessageType.Text) { await OnMessage(message); } else if (type == MessageType.Binary) { await OnBinaryMessage(buffer); } else if (type == MessageType.Close) { await OnDisconnected(CloseStatusCode.Normal, message); onDisconnectedInvoked = true; break; } else { throw new ArgumentOutOfRangeException(); } } _connectionManager.RemoveSession(this); if (!onDisconnectedInvoked) { await OnDisconnected(CloseStatusCode.Normal, "Socket closed"); } }
public void TestDiconnectCallsWebSocketCloseAsync() { WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081"); IWebSocket webSocket = WebSocketFakesFactory.CreateWebSocketThatConnectsAndClosesSuccessful(); webSocketJetConnection.SetWebSocket(webSocket); webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0); webSocketJetConnection.Disconnect(); A.CallTo(() => webSocket.CloseAsync(CloseStatusCode.Away)).MustHaveHappened(Repeated.Exactly.Once); }
private void OnGUI() { var scale = Screen.width / 800f; GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scale, scale, 1)); var width = GUILayout.Width(Screen.width / scale - 10); WebSocketState state = socket == null ? WebSocketState.Closed : socket.ReadyState; GUILayout.Label("SDK Version: 2.5.0", width); var stateColor = state == WebSocketState.Closed ? "red" : state == WebSocketState.Open ? "#11ff11" : "#aa4444"; var richText = new GUIStyle() { richText = true }; GUILayout.Label(string.Format(" <color=white>State:</color> <color={1}>{0}</color>", state, stateColor), richText); GUI.enabled = state == WebSocketState.Closed; GUILayout.Label("Address: ", width); address = GUILayout.TextField(address, width); GUILayout.BeginHorizontal(); GUI.enabled = state == WebSocketState.Closed; if (GUILayout.Button(state == WebSocketState.Connecting ? "Connecting..." : "Connect")) { socket = new WebSocket(address); socket.OnOpen += Socket_OnOpen; socket.OnMessage += Socket_OnMessage; socket.OnClose += Socket_OnClose; socket.OnError += Socket_OnError; AddLog(string.Format("Connecting...\n")); socket.ConnectAsync(); } GUI.enabled = state == WebSocketState.Open; if (GUILayout.Button(state == WebSocketState.Closing ? "Closing..." : "Close")) { AddLog(string.Format("Closing...\n")); socket.CloseAsync(); } GUILayout.EndHorizontal(); GUILayout.Label("Text: "); sendText = GUILayout.TextArea(sendText, GUILayout.MinHeight(50), width); GUILayout.BeginHorizontal(); if (GUILayout.Button("Send")) { if (!string.IsNullOrEmpty(sendText)) { socket.SendAsync(sendText); if (logMessage) { AddLog(string.Format("Send: {0}\n", sendText)); } sendCount += 1; } } if (GUILayout.Button("Send Bytes")) { if (!string.IsNullOrEmpty(sendText)) { var bytes = System.Text.Encoding.UTF8.GetBytes(sendText); socket.SendAsync(bytes); if (logMessage) { AddLog(string.Format("Send Bytes ({1}): {0}\n", sendText, bytes.Length)); } sendCount += 1; } } if (GUILayout.Button("Send x100")) { if (!string.IsNullOrEmpty(sendText)) { for (int i = 0; i < 100; i++) { var text = (i + 1).ToString() + ". " + sendText; socket.SendAsync(text); if (logMessage) { AddLog(string.Format("Send: {0}\n", text)); } sendCount += 1; } } } if (GUILayout.Button("Send Bytes x100")) { if (!string.IsNullOrEmpty(sendText)) { for (int i = 0; i < 100; i++) { var text = (i + 1).ToString() + ". " + sendText; var bytes = System.Text.Encoding.UTF8.GetBytes(text); socket.SendAsync(bytes); if (logMessage) { AddLog(string.Format("Send Bytes ({1}): {0}\n", text, bytes.Length)); } sendCount += 1; } } } GUILayout.EndHorizontal(); GUI.enabled = true; GUILayout.BeginHorizontal(); logMessage = GUILayout.Toggle(logMessage, "Log Message"); GUILayout.Label(string.Format("Send Count: {0}", sendCount)); GUILayout.Label(string.Format("Receive Count: {0}", receiveCount)); GUILayout.EndHorizontal(); if (GUILayout.Button("Clear")) { log = ""; receiveCount = 0; sendCount = 0; } scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 270), width); GUILayout.Label(log); GUILayout.EndScrollView(); }
public void CloseAsync() { _rawSocket.CloseAsync(); }
/// <summary> /// Closes connection to a streaming API. /// </summary> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>Awaitable task object for handling action completion in asynchronous mode.</returns> public Task DisconnectAsync( CancellationToken cancellationToken = default) => _webSocket.CloseAsync(cancellationToken);
private void OnGUI() { var scale = Screen.width / 800f; GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scale, scale, 1)); var width = GUILayout.Width(Screen.width / scale - 10); WebSocketState state = socket == null ? WebSocketState.Closed : socket.ReadyState; GUILayout.BeginHorizontal(); GUILayout.Label("SDK Version: " + Settings.VERSION, GUILayout.Width(Screen.width / scale - 100)); GUI.color = green; GUILayout.Label($"FPS: {fps:F2}", GUILayout.Width(80)); GUI.color = Color.white; GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("State: ", GUILayout.Width(36)); GUI.color = WebSocketState.Closed == state ? red : WebSocketState.Open == state ? green : wait; GUILayout.Label($"{state}", GUILayout.Width(120)); GUI.color = Color.white; GUILayout.EndHorizontal(); GUI.enabled = state == WebSocketState.Closed; GUILayout.Label("Address: ", width); address = GUILayout.TextField(address, width); GUILayout.BeginHorizontal(); GUI.enabled = state == WebSocketState.Closed; if (GUILayout.Button(state == WebSocketState.Connecting ? "Connecting..." : "Connect")) { socket = new WebSocket(address); socket.OnOpen += Socket_OnOpen; socket.OnMessage += Socket_OnMessage; socket.OnClose += Socket_OnClose; socket.OnError += Socket_OnError; AddLog(string.Format("Connecting...")); socket.ConnectAsync(); } GUI.enabled = state == WebSocketState.Open; if (GUILayout.Button(state == WebSocketState.Closing ? "Closing..." : "Close")) { AddLog(string.Format("Closing...")); socket.CloseAsync(); } GUILayout.EndHorizontal(); GUILayout.Label("Message: "); sendText = GUILayout.TextArea(sendText, GUILayout.MinHeight(50), width); GUILayout.BeginHorizontal(); if (GUILayout.Button("Send") && !string.IsNullOrEmpty(sendText)) { socket.SendAsync(sendText); AddLog(string.Format("Send: {0}", sendText)); sendCount += 1; } if (GUILayout.Button("Send Bytes") && !string.IsNullOrEmpty(sendText)) { var bytes = System.Text.Encoding.UTF8.GetBytes(sendText); socket.SendAsync(bytes); AddLog(string.Format("Send Bytes ({1}): {0}", sendText, bytes.Length)); sendCount += 1; } if (GUILayout.Button("Send x100") && !string.IsNullOrEmpty(sendText)) { for (int i = 0; i < 100; i++) { var text = (i + 1).ToString() + ". " + sendText; socket.SendAsync(text); AddLog(string.Format("Send: {0}", text)); sendCount += 1; } } if (GUILayout.Button("Send Bytes x100") && !string.IsNullOrEmpty(sendText)) { for (int i = 0; i < 100; i++) { var text = (i + 1).ToString() + ". " + sendText; var bytes = System.Text.Encoding.UTF8.GetBytes(text); socket.SendAsync(bytes); AddLog(string.Format("Send Bytes ({1}): {0}", text, bytes.Length)); sendCount += 1; } } GUILayout.EndHorizontal(); GUI.enabled = true; GUILayout.BeginHorizontal(); logMessage = GUILayout.Toggle(logMessage, "Log Message"); GUILayout.Label(string.Format("Send Count: {0}", sendCount)); GUILayout.Label(string.Format("Receive Count: {0}", receiveCount)); GUILayout.EndHorizontal(); if (GUILayout.Button("Clear")) { log = ""; receiveCount = 0; sendCount = 0; } scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 270), width); GUILayout.Label(log); GUILayout.EndScrollView(); }
protected Task CloseAsync(CloseStatusCode statusCode, string reason) { return(_webSocket.CloseAsync((int)statusCode, reason)); }
/// <summary> /// Closes connection to a streaming API. /// </summary> /// <returns>Awaitable task object for handling action completion in asynchronous mode.</returns> public Task DisconnectAsync() => _webSocket.CloseAsync();