当前位置: 首页>>代码示例>>C#>>正文


C# WebSocket.SendAsync方法代码示例

本文整理汇总了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);
            }
        }
开发者ID:fakedob,项目名称:KestrelHttpServer,代码行数:25,代码来源:Startup.cs

示例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);
        }
开发者ID:streamcode9,项目名称:chat,代码行数:7,代码来源:WebSocketsHelper.cs

示例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);
     }
 }
开发者ID:marviobezerra,项目名称:XCommon,代码行数:8,代码来源:ConnectionManager.cs

示例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);
 }
开发者ID:TerabyteX,项目名称:WebSockets,代码行数:11,代码来源:Startup.cs

示例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);
     }
 }
开发者ID:hpatel98,项目名称:SCAMP,代码行数:11,代码来源:Program.cs

示例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);
        }
开发者ID:leloulight,项目名称:Entropy,代码行数:15,代码来源:Startup.cs

示例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);
		}
开发者ID:CobaCitizen,项目名称:MyDrives,代码行数:18,代码来源:SocketClient.cs

示例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);
                }
            }
        }
开发者ID:Microsoft,项目名称:RTVS,代码行数:24,代码来源:WebSocketHelper.cs

示例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);
    }
开发者ID:ibebbs,项目名称:Rxx,代码行数:6,代码来源:WebSocketLab.cs

示例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);
     }
 }
开发者ID:streamcode9,项目名称:MicroCMS,代码行数:12,代码来源:BaseHub.cs

示例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);
 }
开发者ID:kallex,项目名称:Caloom,代码行数:5,代码来源:WebSocketHandler.cs

示例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);
 }
开发者ID:kallex,项目名称:Caloom,代码行数:5,代码来源:WebSocketHandler.cs

示例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);
            }
        }
开发者ID:Microsoft,项目名称:RTVS,代码行数:14,代码来源:WebSocketPipeAction.cs

示例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.");
            }
        }
开发者ID:CforED,项目名称:Node.js-Tools-for-Visual-Studio,代码行数:18,代码来源:WebSocketProxyBase.cs

示例15: WebSocketSendAsync

 private static WebSocketSendAsync WebSocketSendAsync(WebSocket webSocket)
 {
     return (buffer, messageType, endOfMessage, cancel) =>
         webSocket.SendAsync(buffer, OpCodeToEnum(messageType), endOfMessage, cancel);
 }
开发者ID:prabirshrestha,项目名称:socket-io-net,代码行数:5,代码来源:SimpleOwinAspNetHost.cs


注:本文中的System.Net.WebSockets.WebSocket.SendAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。