private static async Task WebSocketEcho(IWebSocketContext webSocketContext) { var buffer = new byte[1024]; IWebSocketReceiveResult received = await webSocketContext.Receive(new ArraySegment <byte>(buffer)); while (!webSocketContext.ClientClosed) { await webSocketContext.Send( new ArraySegment <byte>(buffer, 0, received.Count), received.MessageType, received.EndOfMessage); received = await webSocketContext.Receive(new ArraySegment <byte>(buffer)); } await webSocketContext.Close(); }
private static async Task WebSocketReverseMessage(IWebSocketContext webSocketContext) { var buffer = new byte[1024]; IWebSocketReceiveResult received = await webSocketContext.Receive(new ArraySegment <byte>(buffer)); while (!webSocketContext.ClientClosed) { byte[] reversedMessage = Encoding.UTF8.GetBytes(String.Concat(Encoding.UTF8.GetString(buffer, 0, received.Count).Reverse())); await webSocketContext.Send( new ArraySegment <byte>(reversedMessage, 0, reversedMessage.Length), received.MessageType, received.EndOfMessage); received = await webSocketContext.Receive(new ArraySegment <byte>(buffer)); } await webSocketContext.Close(); }
private static async Task WebSocketCheckEnv(IWebSocketContext webSocketContext) { var buffer = new byte[1024]; IWebSocketReceiveResult received = await webSocketContext.Receive(new ArraySegment <byte>(buffer)); while (!webSocketContext.ClientClosed) { bool haveEnv = webSocketContext.Environment != null; byte[] bytes = Encoding.UTF8.GetBytes(haveEnv.ToString()); await webSocketContext.Send( new ArraySegment <byte>(bytes, 0, bytes.Length), received.MessageType, received.EndOfMessage); received = await webSocketContext.Receive(new ArraySegment <byte>(buffer)); } await webSocketContext.Close(); }