1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| while (webSocket.State == WebSocketState.Open){ ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]); WebSocketReceiveResult result = await webSocket.ReceiveAsync(buffer,CancellationToken.None); if (result.MessageType == WebSocketMessageType.Close){ await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); }else{ string message = System.Text.Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
ArraySegment buffer = new ArraySegment(Encoding.UTF8.GetBytes("server:"+message)); await webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); } }
|