From cee8e8e7034ab0f959faeba6137454c13555d1ff Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Mon, 16 Sep 2024 16:41:18 -0600 Subject: [PATCH] feat: adds console command to remove all tokens --- .../MobileControlWebsocketServer.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/4-series/epi-essentials-mobile-control/WebSocketServer/MobileControlWebsocketServer.cs b/4-series/epi-essentials-mobile-control/WebSocketServer/MobileControlWebsocketServer.cs index 6d8e929..4e706e3 100644 --- a/4-series/epi-essentials-mobile-control/WebSocketServer/MobileControlWebsocketServer.cs +++ b/4-series/epi-essentials-mobile-control/WebSocketServer/MobileControlWebsocketServer.cs @@ -354,6 +354,7 @@ private void AddConsoleCommands() CrestronConsole.AddNewConsoleCommand(GenerateClientTokenFromConsole, "MobileAddUiClient", "Adds a client and generates a token. ? for more help", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(RemoveToken, "MobileRemoveUiClient", "Removes a client. ? for more help", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand((s) => PrintClientInfo(), "MobileGetClientInfo", "Displays the current client info", ConsoleAccessLevelEnum.AccessOperator); + CrestronConsole.AddNewConsoleCommand(RemoveAllTokens, "MobileRemoveAllUiClients", "Removes all clients", ConsoleAccessLevelEnum.AccessOperator); } @@ -794,6 +795,40 @@ private void GenerateClientTokenFromConsole(string s) return (key, path); } + /// + /// Removes all clients from the server + /// + private void RemoveAllTokens(string s) + { + if (s == "?" || string.IsNullOrEmpty(s)) + { + CrestronConsole.ConsoleCommandResponse(@"Removes all clients from the server"); + return; + } + + foreach (var client in UiClients) + { + if (client.Value.Client != null && client.Value.Client.Context.WebSocket.IsAlive) + { + client.Value.Client.Context.WebSocket.Close(CloseStatusCode.Normal, "Server Shutting Down"); + } + + var path = _wsPath + client.Key; + if (_server.RemoveWebSocketService(path)) + { + UiClients.Remove(client.Key); + + CrestronConsole.ConsoleCommandResponse(string.Format("Client removed with token: {0}", client.Key)); + } + else + { + CrestronConsole.ConsoleCommandResponse(string.Format("Unable to remove client with token : {0}", client.Key)); + } + } + + UpdateSecret(); + } + /// /// Removes a client with the specified token value ///