/// <summary> /// Continuously reads data from the websocket. /// </summary> public async Task HandleSocket() { try { var buffer = new byte[Utilities.Server.ReceiveBufferSize]; WebSocketReceiveResult result = await ClientSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); await ParseMessage(result, buffer); while (!ClientSocket.CloseStatus.HasValue) { buffer = new byte[Utilities.Server.ReceiveBufferSize]; result = await ClientSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); await ParseMessage(result, buffer); } if (Utilities.Server.ClientList.Contains(this)) { Utilities.Server.ClientList.Remove(this); } await ClientSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); } catch (Exception ex) { if (Utilities.Server.ClientList.Contains(this)) { Utilities.Server.ClientList.Remove(this); } await ClientSocket.CloseOutputAsync(WebSocketCloseStatus.InternalServerError, "An unhandled exception occurred.", CancellationToken.None); SocketError?.Invoke(this, ex); SocketClosed?.Invoke(this, EventArgs.Empty); ClientSocket.Dispose(); } }