using GeoSigmaDrawLib; using Swan; using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Threading; namespace KevDrawServer { class Program { private static readonly string ServiceName = "EmbedIOWebsocketService"; public static int Main(string[] args) { var mutex = new Mutex(false, ServiceName); try { // Makes sure only one instance of the Tray app is started if (mutex.WaitOne(0, false)) { var mainProgram = new Program(); mainProgram.RunServer(); Terminal.WriteLine("Service has closed"); } else { Terminal.WriteLine($@"{ServiceName} already running"); } } catch (Exception e) { Terminal.WriteLine("Error in Service " + e.Message); } finally { mutex.Close(); } return 0; } private void RunServer() { try { Security security = new Security(); security.CheckLicense(null); } catch { return; } string strPort = WebConfig.ServicePort; List lstUrl = new List(); // 获取本地主机名 string hostName = Dns.GetHostName(); // 获取本地主机的所有IP地址 IPAddress[] addresses = Dns.GetHostAddresses(hostName); foreach (IPAddress address in addresses) { // 只输出IPv4地址,忽略IPv6地址(如果需要的话) if (address.AddressFamily == AddressFamily.InterNetwork) // AddressFamily.InterNetwork 表示IPv4 { lstUrl.Add($"http://{address}:{strPort}"); //Console.WriteLine($"IP Address: {address.ToString()}"); } } if(!lstUrl.Contains($"http://127.0.0.1:{strPort}")) { lstUrl.Add($"http://127.0.0.1:{strPort}"); } if (!lstUrl.Contains($"http://localhost:{strPort}")) { lstUrl.Add($"http://localhost:{strPort}"); } //ServerService service = new ServerService("ws://0.0.0.0:8080"); //ServerService service = new ServerService("http://localhost:8080"); //ServerService service = new ServerService("http://192.168.31.113:8080"); //ServerService service = new ServerService(WebConfig.ServiceIP); ServerService service = new ServerService(lstUrl); service.Start(); // Wait for any key to be pressed before disposing of our web server. // In a service, we'd manage the lifecycle of our web server using // something like a BackgroundWorker or a ManualResetEvent. Console.ReadKey(true); } } }