Innovenergy_trunk/csharp/App/Middleware/Program.cs

46 lines
1.9 KiB
C#

using InnovEnergy.App.Middleware;
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
internal class Program
{
public static readonly object SharedDataLock = new object();
public static void Main(string[] args)
{
//For each installation id, we maintain a list of the connected clients
var installationConnections = new Dictionary<int, InstallationInfo>();
var installationsIds = new List<int> {1};
var installationIps = new List<string> {"10.2.3.115"};
RabbitMqConsumer.StartRabbitMqConsumer(installationConnections,SharedDataLock);
UdpClient udpClient = new UdpClient();
int port = 9000;
for (int i = 0; i < installationsIds.Count; i++)
{
using (udpClient)
{
// Convert the message to bytes (UTF-8 encoding is used here)
string message = "This is a message from RabbitMQ server, you can subscribe to the RabbitMQ queue";
byte[] data = Encoding.UTF8.GetBytes(message);
// Send the UDP message to the specified IP address and port
udpClient.Send(data, data.Length, installationIps[i], port);
Console.WriteLine($"Sent UDP message to {installationIps[i]}:{port}: {message}");
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(installationIps[i]), port);
byte[] replyData = udpClient.Receive(ref remoteEndPoint);
string replyMessage = Encoding.UTF8.GetString(replyData);
Console.WriteLine("Received message from installation "+installationsIds[i]);
}
}
WebSocketListener.StartServerAsync(installationConnections,SharedDataLock);
Console.WriteLine("WebSocket server is running. Press Enter to exit.");
Console.ReadLine();
}
}