public CloseOutputAsync ( System closeStatus, string statusDescription, System cancellationToken ) : System.Threading.Tasks.Task | ||
closeStatus | System | |
statusDescription | string | |
cancellationToken | System | |
리턴 | System.Threading.Tasks.Task |
private void disconnect() { try { clientWebSocket.CloseOutputAsync(WebSocketCloseStatus.Empty, null, CancellationToken.None); } catch (Exception) { // ignore } }
public async Task EndToEnd_ConnectAndClose_Success() { OwinHttpListener listener = CreateServer(env => { var accept = (WebSocketAccept)env["websocket.Accept"]; Assert.NotNull(accept); accept( null, async wsEnv => { var sendAsync1 = wsEnv.Get<WebSocketSendAsync>("websocket.SendAsync"); var receiveAsync1 = wsEnv.Get<WebSocketReceiveAsync>("websocket.ReceiveAsync"); var closeAsync1 = wsEnv.Get<WebSocketCloseAsync>("websocket.CloseAsync"); var buffer1 = new ArraySegment<byte>(new byte[10]); await receiveAsync1(buffer1, CancellationToken.None); await closeAsync1((int)WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None); }); return TaskHelpers.Completed(); }, HttpServerAddress); using (listener) { using (var client = new ClientWebSocket()) { await client.ConnectAsync(new Uri(WsClientAddress), CancellationToken.None); await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); WebSocketReceiveResult readResult = await client.ReceiveAsync(new ArraySegment<byte>(new byte[10]), CancellationToken.None); Assert.Equal(WebSocketCloseStatus.NormalClosure, readResult.CloseStatus); Assert.Equal("Closing", readResult.CloseStatusDescription); Assert.Equal(0, readResult.Count); Assert.True(readResult.EndOfMessage); Assert.Equal(WebSocketMessageType.Close, readResult.MessageType); } } }
/// <summary>Close the connection.</summary> /// <param name="timeout">The maximum timeout in milliseconds for the function to execute. Will raise exception when timeout is reached.</param> internal async System.Threading.Tasks.Task Close(int timeout) { // [GOODBYE, Details|dict, Reason|uri] try { await Send($"[{(int)Messages.GOODBYE},{{}},\"bye_from_csharp_client\"]", timeout); Response response = await ReceiveExpect(Messages.GOODBYE, 0, timeout); stopServerTokenSource.Cancel(); using (var cts = new System.Threading.CancellationTokenSource(timeout)) { await ws.CloseOutputAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "wamp_close", cts.Token); } } catch (System.Net.WebSockets.WebSocketException) { ws.Dispose(); stopServerTokenSource.Cancel(); return; } }
public async Task SubProtocol_SelectLastSubProtocol_Success() { OwinHttpListener listener = CreateServer(env => { var accept = (WebSocketAccept)env["websocket.Accept"]; Assert.NotNull(accept); var requestHeaders = env.Get<IDictionary<string, string[]>>("owin.RequestHeaders"); var responseHeaders = env.Get<IDictionary<string, string[]>>("owin.ResponseHeaders"); // Select the last sub-protocol from the client. string subProtocol = requestHeaders["Sec-WebSocket-Protocol"].Last().Split(',').Last().Trim(); responseHeaders["Sec-WebSocket-Protocol"] = new string[] { subProtocol + "A" }; accept( new Dictionary<string, object>() { { "websocket.SubProtocol", subProtocol } }, async wsEnv => { var sendAsync = wsEnv.Get<WebSocketSendAsync>("websocket.SendAsync"); var receiveAsync = wsEnv.Get<WebSocketReceiveAsync>("websocket.ReceiveAsync"); var closeAsync = wsEnv.Get<WebSocketCloseAsync>("websocket.CloseAsync"); var buffer = new ArraySegment<byte>(new byte[100]); Tuple<int, bool, int> serverReceive = await receiveAsync(buffer, CancellationToken.None); // Assume close received await closeAsync((int)WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None); }); return TaskHelpers.Completed(); }, HttpServerAddress); using (listener) { using (var client = new ClientWebSocket()) { client.Options.AddSubProtocol("protocol1"); client.Options.AddSubProtocol("protocol2"); await client.ConnectAsync(new Uri(WsClientAddress), CancellationToken.None); await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None); var receiveBody = new byte[100]; WebSocketReceiveResult readResult = await client.ReceiveAsync(new ArraySegment<byte>(receiveBody), CancellationToken.None); Assert.Equal(WebSocketMessageType.Close, readResult.MessageType); Assert.Equal("protocol2", client.SubProtocol); } } }
public Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken) { return(webSocket.CloseOutputAsync(closeStatus, statusDescription, cancellationToken)); }
public void AuthorizationTest() { using (WebApp.Start(new StartOptions("http://localhost:8989"), startup => { startup.MapOwinFleckRoute("/fleck", connection => { var id = ConfigureIntegrationTestConnectionAndGetId(connection); connection.OnAuthenticateRequest = () => { var result = id % 2 == 1; Send(id, $"Auth {id}: {result}"); return result; }; }); })) using (var client1 = new ClientWebSocket()) using (var client2 = new ClientWebSocket()) using (var client3 = new ClientWebSocket()) { client1.ConnectAsync(new Uri("ws://localhost:8989/fleck"), CancellationToken.None).Wait(); try { client2.ConnectAsync(new Uri("ws://localhost:8989/fleck"), CancellationToken.None).Wait(); Assert.Fail("Client 2 should not be unauthorized"); } catch (AggregateException ex) { Assert.AreEqual("Unable to connect to the remote server", ex.InnerException.Message); } client3.ConnectAsync(new Uri("ws://localhost:8989/fleck"), CancellationToken.None).Wait(); var bytes3 = new byte[1024]; var segment3 = new ArraySegment<byte>(bytes3); var receive3 = client3.ReceiveAsync(segment3, CancellationToken.None); var message1 = "Hello world"; var bytes1 = Encoding.UTF8.GetBytes(message1); var segment1 = new ArraySegment<byte>(bytes1); client1.SendAsync(segment1, WebSocketMessageType.Text, true, CancellationToken.None).Wait(); var result3 = receive3.Result; Assert.AreEqual(WebSocketMessageType.Text, result3.MessageType); var message3 = Encoding.UTF8.GetString(segment3.Array, 0, result3.Count); Assert.AreEqual(message3, "User 1: Hello world"); client3.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait(); client3.Dispose(); Task.Delay(100).Wait(); client1.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait(); client1.Dispose(); Task.Delay(100).Wait(); } var messages = DequeueMessages(); Assert.AreEqual(8, messages.Count); Assert.AreEqual("Auth 1: True", messages[0]); Assert.AreEqual("Open: 1", messages[1]); Assert.AreEqual("Auth 2: False", messages[2]); Assert.AreEqual("Auth 3: True", messages[3]); Assert.AreEqual("Open: 3", messages[4]); Assert.AreEqual("User 1: Hello world", messages[5]); Assert.AreEqual("Close: 3", messages[6]); Assert.AreEqual("Close: 1", messages[7]); }
protected void SendIntegrationTestMessages() { using (var client1 = new ClientWebSocket()) using (var client2 = new ClientWebSocket()) { client1.ConnectAsync(new Uri("ws://localhost:8989"), CancellationToken.None).Wait(); Task.Delay(100).Wait(); client2.ConnectAsync(new Uri("ws://localhost:8989"), CancellationToken.None).Wait(); Task.Delay(100).Wait(); var bytes2 = new byte[1024]; var segment2 = new ArraySegment<byte>(bytes2); var receive2 = client2.ReceiveAsync(segment2, CancellationToken.None); var message1 = "Hello world"; var bytes1 = Encoding.UTF8.GetBytes(message1); var segment1 = new ArraySegment<byte>(bytes1); client1.SendAsync(segment1, WebSocketMessageType.Text, true, CancellationToken.None).Wait(); Task.Delay(100).Wait(); var result2 = receive2.Result; Assert.AreEqual(WebSocketMessageType.Text, result2.MessageType); var message3 = Encoding.UTF8.GetString(segment2.Array, 0, result2.Count); Assert.AreEqual("User 1: Hello world", message3); client2.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait(); client2.Dispose(); Task.Delay(100).Wait(); client1.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait(); client1.Dispose(); Task.Delay(100).Wait(); } }