diff --git a/csharp/App/Backend/Controller.cs b/csharp/App/Backend/Controller.cs index e76f2d71d..ee4d50ba1 100644 --- a/csharp/App/Backend/Controller.cs +++ b/csharp/App/Backend/Controller.cs @@ -77,101 +77,7 @@ public class Controller : ControllerBase var webSocket = webSocketContext; //Handle the WebSocket connection - //WebsocketManager.HandleWebSocketConnection(webSocket) - - var buffer = new byte[4096]; - try - { - while (webSocket.State == WebSocketState.Open) - { - //Listen for incoming messages on this WebSocket - var result = await webSocket.ReceiveAsync(buffer, CancellationToken.None); - Console.WriteLine("Received a new message from websocket"); - if (result.MessageType != WebSocketMessageType.Text) - continue; - - var message = Encoding.UTF8.GetString(buffer, 0, result.Count); - var installationIds = JsonSerializer.Deserialize(message); - - lock (WebsocketManager.InstallationConnections) - { - //Each front-end will send the list of the installations it wants to access - //If this is a new key (installation id), initialize the list for this key and then add the websocket object for this client - //Then, report the status of each requested installation to the front-end that created the websocket connection - if (installationIds != null) - foreach (var installationId in installationIds) - { - if (!WebsocketManager.InstallationConnections.ContainsKey(installationId)) - { - Console.WriteLine("Create new empty list for installation id " + installationId); - WebsocketManager.InstallationConnections[installationId] = new InstallationInfo - { - Status = -2 - }; - } - - WebsocketManager.InstallationConnections[installationId].Connections.Add(webSocket); - - var jsonObject = new - { - id = installationId, - status = WebsocketManager.InstallationConnections[installationId].Status - }; - - var jsonString = JsonSerializer.Serialize(jsonObject); - var dataToSend = Encoding.UTF8.GetBytes(jsonString); - - - webSocket.SendAsync(dataToSend, - WebSocketMessageType.Text, - true, // Indicates that this is the end of the message - CancellationToken.None - ); - } - - Console.WriteLine("Printing installation connection list"); - Console.WriteLine("----------------------------------------------"); - foreach (var installationConnection in WebsocketManager.InstallationConnections) - { - Console.WriteLine("Installation ID: " + installationConnection.Key + " Number of Connections: " + installationConnection.Value.Connections.Count); - } - Console.WriteLine("----------------------------------------------"); - } - } - - lock (WebsocketManager.InstallationConnections) - { - //When the front-end terminates the connection, the following code will be executed - Console.WriteLine("The connection has been terminated"); - foreach (var installationConnection in WebsocketManager.InstallationConnections) - { - if (installationConnection.Value.Connections.Contains(webSocket)) - { - installationConnection.Value.Connections.Remove(webSocket); - } - } - } - - await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Connection closed by server", CancellationToken.None); - lock (WebsocketManager.InstallationConnections) - { - //Print the installationConnections dictionary after deleting a websocket - Console.WriteLine("Print the installation connections list after deleting a websocket"); - Console.WriteLine("----------------------------------------------"); - foreach (var installationConnection in WebsocketManager.InstallationConnections) - { - Console.WriteLine("Installation ID: " + installationConnection.Key + " Number of Connections: " + installationConnection.Value.Connections.Count); - } - - Console.WriteLine("----------------------------------------------"); - } - } - catch (Exception ex) - { - Console.WriteLine("WebSocket error: " + ex.Message); - } - - + await WebsocketManager.HandleWebSocketConnection(webSocket); } [HttpGet(nameof(GetUserById))] diff --git a/csharp/App/Backend/Websockets/WebsockerManager.cs b/csharp/App/Backend/Websockets/WebsockerManager.cs index 37e5d4e85..591bb1abf 100644 --- a/csharp/App/Backend/Websockets/WebsockerManager.cs +++ b/csharp/App/Backend/Websockets/WebsockerManager.cs @@ -67,8 +67,8 @@ public static class WebsocketManager public static void StartRabbitMqConsumer() { - //string vpnServerIp = "194.182.190.208"; - string vpnServerIp = "127.0.0.1"; + string vpnServerIp = "194.182.190.208"; + //string vpnServerIp = "127.0.0.1"; _factory = new ConnectionFactory { HostName = vpnServerIp}; _connection = _factory.CreateConnection(); _channel = _connection.CreateModel();