public async Task ProcessWebSocket(HttpContext context, WebSocket webSocket)
{
var connection = new Connection(context, webSocket);
Console.WriteLine("Connect: {0}", context.Connection.RemoteIpAddress);
var cancelToken = CancellationToken.None;
var buffer = new byte[1024];
WebSocketReceiveResult received =
await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancelToken);
while (!webSocket.CloseStatus.HasValue)
{
string text = System.Text.Encoding.UTF8.GetString(buffer, 0, received.Count);
Console.WriteLine("Recd: {0}", text);
try
{
Cmd.Parse(text, connection)?.Run();
}
catch (Exception ex)
{
Console.WriteLine("Unhandled exception: {0}", ex);
}
received = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancelToken);
}
await webSocket.CloseAsync(webSocket.CloseStatus.Value, webSocket.CloseStatusDescription, cancelToken);
}