本文整理汇总了C#中System.Net.WebSockets.ClientWebSocket.CloseOutputAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ClientWebSocket.CloseOutputAsync方法的具体用法?C# ClientWebSocket.CloseOutputAsync怎么用?C# ClientWebSocket.CloseOutputAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.WebSockets.ClientWebSocket
的用法示例。
在下文中一共展示了ClientWebSocket.CloseOutputAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EndToEnd_ConnectAndClose_Success
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);
}
}
}
示例2: AuthorizationTest
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]);
}
示例3: SubProtocol_SelectLastSubProtocol_Success
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);
}
}
}
示例4: SendIntegrationTestMessages
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();
}
}