本文整理汇总了C#中System.Net.WebSockets.WebSocket.SendAsync方法的典型用法代码示例。如果您正苦于以下问题:C# WebSocket.SendAsync方法的具体用法?C# WebSocket.SendAsync怎么用?C# WebSocket.SendAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.WebSockets.WebSocket
的用法示例。
在下文中一共展示了WebSocket.SendAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EchoAsync
public async Task EchoAsync(WebSocket webSocket)
{
var buffer = new ArraySegment<byte>(new byte[8192]);
for (; ;)
{
var result = await webSocket.ReceiveAsync(
buffer,
CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
return;
}
else if (result.MessageType == WebSocketMessageType.Text)
{
Console.WriteLine("{0}", System.Text.Encoding.UTF8.GetString(buffer.Array, 0, result.Count));
}
await webSocket.SendAsync(
new ArraySegment<byte>(buffer.Array, 0, result.Count),
result.MessageType,
result.EndOfMessage,
CancellationToken.None);
}
}
示例2: SendMessage
private static async Task SendMessage(WebSocket webSocket, string msg, bool endOfMessage)
{
logger.LogVerbose("SendMessage = {0}", msg);
var msgBytes = Encoding.UTF8.GetBytes(msg);
await webSocket.SendAsync(new ArraySegment<byte>(msgBytes, 0, msgBytes.Length), WebSocketMessageType.Text, endOfMessage, CancellationToken.None);
}
示例3: Send
private async Task Send(WebSocket webSocket, SocketMessage message)
{
if (webSocket != null && webSocket.State == WebSocketState.Open)
{
var value = new ArraySegment<byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)));
await webSocket.SendAsync(value, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
示例4: Echo
private async Task Echo(WebSocket webSocket)
{
byte[] buffer = new byte[1024 * 4];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
while (!result.CloseStatus.HasValue)
{
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}
示例5: SendAsync
static async Task SendAsync(WebSocket ws, Message m)
{
using (MemoryStream mem = new MemoryStream())
using (StreamWriter sw = new StreamWriter(mem))
{
await sw.WriteAsync(JsonConvert.SerializeObject(m));
await sw.FlushAsync();
var seg = new ArraySegment<byte>(mem.GetBuffer(), 0, (int)mem.Length);
await ws.SendAsync(seg, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
示例6: EchoWebSocket
private async Task EchoWebSocket(WebSocket webSocket)
{
byte[] buffer = new byte[1024];
WebSocketReceiveResult received = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
while (!webSocket.CloseStatus.HasValue)
{
// Echo anything we receive
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, received.Count), received.MessageType, received.EndOfMessage, CancellationToken.None);
received = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseAsync(webSocket.CloseStatus.Value, webSocket.CloseStatusDescription, CancellationToken.None);
}
示例7: Execute
public async void Execute(HttpListenerContext context, EventSocketExecuteResult callback){
WebSocketContext webSocketContext = await context.AcceptWebSocketAsync(null);
_ws = webSocketContext.WebSocket;
for (int i = 0; i != 10; ++i)
{
// await Task.Delay(20);
var time = DateTime.Now.ToLongTimeString();
var buffer = Encoding.UTF8.GetBytes(time);
var segment = new ArraySegment<byte>(buffer);
await _ws.SendAsync(segment, System.Net.WebSockets.WebSocketMessageType.Text,
true, CancellationToken.None);
}
if (callback!=null) {
callback (true);
}
// await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "Done", CancellationToken.None);
}
示例8: DoWebSocketReceiveSendAsync
private static async Task DoWebSocketReceiveSendAsync(WebSocket receiver, WebSocket sender, CancellationToken ct) {
if (receiver == null || sender == null) {
return;
}
ArraySegment<byte> receiveBuffer = new ArraySegment<byte>(new byte[65335]);
while (receiver.State == WebSocketState.Open && sender.State == WebSocketState.Open) {
if (ct.IsCancellationRequested) {
receiver.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal closure", CancellationToken.None).SilenceException<WebSocketException>().DoNotWait();
sender.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal closure", CancellationToken.None).SilenceException<WebSocketException>().DoNotWait();
return;
}
WebSocketReceiveResult result = await receiver.ReceiveAsync(receiveBuffer, ct);
byte[] data = await receiveBuffer.ToByteArrayAsync(result.Count);
ArraySegment<byte> sendBuffer = new ArraySegment<byte>(data);
await sender.SendAsync(sendBuffer, result.MessageType, result.EndOfMessage, ct);
if (result.MessageType == WebSocketMessageType.Close) {
await receiver.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal closure", ct);
}
}
}
示例9: SendMessageAsync
private static Task SendMessageAsync(string message, WebSocket socket, CancellationToken cancel)
{
var buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(message));
return socket.SendAsync(buffer, WebSocketMessageType.Text, true, cancel);
}
示例10: SendMessage
private static async Task SendMessage(WebSocket webSocket, string message)
{
var msgBytes = Encoding.UTF8.GetBytes(message);
try
{
await webSocket.SendAsync(new ArraySegment<byte>(msgBytes, 0, msgBytes.Length), WebSocketMessageType.Text, true, CancellationToken.None);
}
catch (Exception ex)
{
logger.LogVerbose("Exception in SendMessage: {0}", ex.Message);
}
}
示例11: SendBinaryMessage
private async static Task SendBinaryMessage(WebSocket socket, byte[] binaryMessage)
{
ArraySegment<byte> outputBuffer = new ArraySegment<byte>(binaryMessage);
await socket.SendAsync(outputBuffer, WebSocketMessageType.Binary, true, CancellationToken.None);
}
示例12: SendTextMessage
private async static Task SendTextMessage(WebSocket socket, string textMessage)
{
ArraySegment<byte> outputBuffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(textMessage));
await socket.SendAsync(outputBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
}
示例13: PipeToWebSocketWorker
private static async Task PipeToWebSocketWorker(WebSocket socket, IMessagePipeEnd pipe, CancellationToken cancellationToken) {
while (true) {
cancellationToken.ThrowIfCancellationRequested();
byte[] message;
try {
message = await pipe.ReadAsync(cancellationToken);
} catch (PipeDisconnectedException) {
break;
}
await socket.SendAsync(new ArraySegment<byte>(message, 0, message.Length), WebSocketMessageType.Binary, true, cancellationToken);
}
}
示例14: CopyFromStreamToWebSocketWorker
private async Task CopyFromStreamToWebSocketWorker(Stream stream, WebSocket webSocket, CancellationToken ct) {
var buffer = new byte[0x10000];
while (webSocket.State == WebSocketState.Open) {
ct.ThrowIfCancellationRequested();
Log("TCP -> WS: waiting for packet.");
int count = await stream.ReadAsync(buffer, 0, buffer.Length, ct);
Log("TCP -> WS: received packet:\n{0}", Encoding.UTF8.GetString(buffer, 0, count));
if (count == 0) {
Log("TCP -> WS: zero-length TCP packet received, connection closed.");
break;
}
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, count), WebSocketMessageType.Binary, true, ct);
Log("TCP -> WS: packet relayed.");
}
}
示例15: WebSocketSendAsync
private static WebSocketSendAsync WebSocketSendAsync(WebSocket webSocket)
{
return (buffer, messageType, endOfMessage, cancel) =>
webSocket.SendAsync(buffer, OpCodeToEnum(messageType), endOfMessage, cancel);
}