mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-20 19:19:39 +08:00
添加 Ctrl+C 事件
This commit is contained in:
parent
d19d8f5e95
commit
3f27123c03
@ -16,6 +16,13 @@ HTTPListener? WebSocketListener = null;
|
||||
|
||||
StartServer();
|
||||
|
||||
Console.CancelKeyPress += (sender, e) =>
|
||||
{
|
||||
e.Cancel = true; // 防止程序立即退出
|
||||
CloseServer();
|
||||
Environment.Exit(0); // 退出程序
|
||||
};
|
||||
|
||||
while (Running)
|
||||
{
|
||||
string order = Console.ReadLine() ?? "";
|
||||
@ -29,6 +36,7 @@ while (Running)
|
||||
case OrderDictionary.Exit:
|
||||
case OrderDictionary.Close:
|
||||
Running = false;
|
||||
CloseServer();
|
||||
break;
|
||||
case OrderDictionary.Restart:
|
||||
if (SocketListener is null || WebSocketListener is null)
|
||||
@ -93,7 +101,7 @@ void StartServer()
|
||||
ServerHelper.GetServerSettings();
|
||||
Console.Title = Config.ServerName + " - FunGame Server Port: " + Config.ServerPort;
|
||||
}
|
||||
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
|
||||
ServerHelper.WriteLine("请输入 help 来获取帮助,按下 Ctrl+C 关闭服务器。");
|
||||
|
||||
ServerHelper.PrintFunGameTitle();
|
||||
|
||||
@ -261,3 +269,8 @@ void StartServer()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void CloseServer()
|
||||
{
|
||||
FunGameSystem.CloseServer();
|
||||
}
|
||||
|
@ -64,10 +64,10 @@ namespace Milimoe.FunGame.Server.Others
|
||||
/// </summary>
|
||||
public static void InitMailSender()
|
||||
{
|
||||
MailSender? sender = null;
|
||||
try
|
||||
{
|
||||
sender = SmtpHelper.GetMailSender();
|
||||
Factory.OpenFactory.RegisterFactory(SmtpHelper.GetMailSender);
|
||||
MailSender? sender = SmtpHelper.GetMailSender();
|
||||
if (sender != null)
|
||||
{
|
||||
ServerHelper.WriteLine("SMTP 服务已启动!");
|
||||
@ -84,6 +84,10 @@ namespace Milimoe.FunGame.Server.Others
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载游戏模组
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool GetGameModuleList()
|
||||
{
|
||||
List<string> supported = [];
|
||||
@ -131,6 +135,9 @@ namespace Milimoe.FunGame.Server.Others
|
||||
return Config.GameModuleSupported.Length > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载服务器插件
|
||||
/// </summary>
|
||||
public static void GetServerPlugins()
|
||||
{
|
||||
Dictionary<string, object> delegates = [];
|
||||
@ -151,6 +158,9 @@ namespace Milimoe.FunGame.Server.Others
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载 Web API 插件
|
||||
/// </summary>
|
||||
public static void GetWebAPIPlugins()
|
||||
{
|
||||
Dictionary<string, object> delegates = [];
|
||||
@ -187,6 +197,10 @@ namespace Milimoe.FunGame.Server.Others
|
||||
sqlHelper.Execute(RoomQuery.Delete_Rooms());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建 SQL 服务后需要做的事
|
||||
/// </summary>
|
||||
/// <param name="sqlHelper"></param>
|
||||
public static void AfterCreateSQLService(SQLHelper sqlHelper)
|
||||
{
|
||||
Config.SQLMode = sqlHelper.Mode;
|
||||
@ -194,5 +208,33 @@ namespace Milimoe.FunGame.Server.Others
|
||||
ClearRoomList(sqlHelper);
|
||||
sqlHelper.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭服务器要做的事
|
||||
/// </summary>
|
||||
public static void CloseServer()
|
||||
{
|
||||
if (Config.GameModuleLoader != null)
|
||||
{
|
||||
foreach (GameModuleServer server in Config.GameModuleLoader.ModuleServers.Values)
|
||||
{
|
||||
server.Controller.Close();
|
||||
}
|
||||
}
|
||||
if (Config.ServerPluginLoader != null)
|
||||
{
|
||||
foreach (ServerPlugin plugin in Config.ServerPluginLoader.Plugins.Values)
|
||||
{
|
||||
plugin.Controller.Close();
|
||||
}
|
||||
}
|
||||
if (Config.WebAPIPluginLoader != null)
|
||||
{
|
||||
foreach (WebAPIPlugin plugin in Config.WebAPIPluginLoader.Plugins.Values)
|
||||
{
|
||||
plugin.Controller.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ try
|
||||
RESTfulAPIListener apiListener = new();
|
||||
RESTfulAPIListener.Instance = apiListener;
|
||||
|
||||
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
|
||||
ServerHelper.WriteLine("请输入 help 来获取帮助,按下 Ctrl+C 关闭服务器。");
|
||||
|
||||
ServerHelper.PrintFunGameTitle();
|
||||
|
||||
@ -210,6 +210,10 @@ try
|
||||
});
|
||||
});
|
||||
|
||||
// 捕捉关闭程序事件
|
||||
IHostApplicationLifetime lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
|
||||
lifetime.ApplicationStopping.Register(CloseServer);
|
||||
|
||||
// 启用 WebSockets 中间件
|
||||
WebSocketOptions webSocketOptions = new()
|
||||
{
|
||||
@ -299,3 +303,8 @@ async Task WebSocketConnectionHandler(HttpContext context)
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
void CloseServer()
|
||||
{
|
||||
FunGameSystem.CloseServer();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user