Skip to main content

WebSocketCommandHandler

A class in Root.Township, Alta.Console.

Server: one remote console connection over WebSocket (Fleck). The first message must be a console JWT; after that each message is a JSON request that runs a console command.

Constructors​

MemberWhat it is
WebSocketCommandHandler(IWebSocketConnection connection)Creates a WebSocketCommandHandler from connection.

Fields​

7 private and internal fields
MemberWhat it is
private TaskCompletionSource<bool> authorizationTaskPrivate: completes with whether the token was accepted. Null until the first message and after close.
private readonly IWebSocketConnection connectionPrivate: the underlying Fleck socket.
private readonly string connectionStringPrivate: the client's IP and port, used in log lines.
private CommandContext contextPrivate: the command context (user plus server permissions) every command from this connection runs with.
private static readonly JsonSerializerSettings jsonSettingsPrivate: JSON settings for responses, with converters for Unity vectors and nullable types.
private static readonly Logger loggerLogger for this class.
private readonly HashSet<string> subscriptionsPrivate: the console event types this connection is subscribed to.

Properties​

MemberWhat it is
static WebSocketCommandHandler Current { get; private set; }The handler whose command is running right now. Set only while RunCommand awaits a result, so subscribe commands can find their caller.
bool IsAuthorized { get; }True once the authorization task finished and the token was accepted.
UserInfoWithPermissions UserInfo { get; private set; }The user and permissions read from the validated token.

Events​

MemberWhat it is
event Action<WebSocketCommandHandler> AuthorizedRaised once the connection's token has been validated and its command context built.
event Action<WebSocketCommandHandler> ClosedRaised when the socket closes, after its event subscriptions are dropped.

Methods​

MemberWhat it is
WebSocketCommandHandler.SubscriptionResult Subscribe(string eventType)Subscribes this connection to a console event type. Returns whether it was added, already there or not found.
WebSocketCommandHandler.SubscriptionResult Unsubscribe(string eventType)Removes a console event subscription from this connection.
12 private and internal methods
MemberWhat it is
private async Task<IEnumerable<GroupPermissions>> GetCurrentServerPermissionsForLoggedInUser()Private: asks the Alta API for the connected user's group permissions on this server.
private void HandleRequest(string message)Private: parses a JSON Request and runs it on the main thread through RunCommand.
private async void OnAuthorize(string message)Private: first message handler. Waits for any running authorization, then either authorizes the token or treats the message as a normal request.
private void OnClose()Private: unsubscribes all console events, removes the message handlers and raises Closed.
private static void OnError(Exception ex)Private: logs socket errors.
private void OnOpen()Private: logs that the connection opened.
private async void ProcessAuthorization(string message)Private: validates the JWT with ServerConsoleManager, loads the user's permissions and switches the socket to HandleRequest. Closes the socket on a bad token.
private void ProcessPreAuthorizedRequest(string message)Private: for a message that reached OnAuthorize after authorization, swaps in HandleRequest and handles it.
private async void RunCommand(WebSocketCommandHandler.Request request)Private: runs the request through CommandService.Handle, raises the command executed console event and sends back the result with the request id.
private void Send(WebSocketCommandHandler.ResponseType type, object data, string eventType = null, uint? commandId = null)Private: serializes a response and sends it. Drops the event subscription if the socket is no longer available.
private void Send(WebSocketCommandHandler.ResponseData response)Private: serializes a response and sends it. Drops the event subscription if the socket is no longer available.
private void TryLogConsoleCommandExecutionWithEventStream(string command, bool wasSuccessful, string output)Private: server only, raises ConsoleEvents.CommandExecuted with the command, success and output.

Used in​

Members of other types that take, return or hold a WebSocketCommandHandler: