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