Update VPN IPs
This commit is contained in:
parent
2199f43400
commit
704c1910db
|
@ -77,101 +77,7 @@ public class Controller : ControllerBase
|
||||||
var webSocket = webSocketContext;
|
var webSocket = webSocketContext;
|
||||||
|
|
||||||
//Handle the WebSocket connection
|
//Handle the WebSocket connection
|
||||||
//WebsocketManager.HandleWebSocketConnection(webSocket)
|
await 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<int[]>(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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet(nameof(GetUserById))]
|
[HttpGet(nameof(GetUserById))]
|
||||||
|
|
|
@ -67,8 +67,8 @@ public static class WebsocketManager
|
||||||
|
|
||||||
public static void StartRabbitMqConsumer()
|
public static void StartRabbitMqConsumer()
|
||||||
{
|
{
|
||||||
//string vpnServerIp = "194.182.190.208";
|
string vpnServerIp = "194.182.190.208";
|
||||||
string vpnServerIp = "127.0.0.1";
|
//string vpnServerIp = "127.0.0.1";
|
||||||
_factory = new ConnectionFactory { HostName = vpnServerIp};
|
_factory = new ConnectionFactory { HostName = vpnServerIp};
|
||||||
_connection = _factory.CreateConnection();
|
_connection = _factory.CreateConnection();
|
||||||
_channel = _connection.CreateModel();
|
_channel = _connection.CreateModel();
|
||||||
|
|
Loading…
Reference in New Issue