You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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()
{
string strPort = WebConfig.ServicePort;
List<string> lstUrl = new List<string>();
// 获取本地主机名
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);
}
}
}