mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-22 12:09:36 +08:00

* 添加 Web API 和 RESTful API 模式; * 添加 SQLite 模式; * 添加 ISocketMessageProcessor 和 ISocketListener<> 接口,用于统一数据访问; * 重做了 ISocketModel; * 完善了 WebSocket 的连接模式。
73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using Milimoe.FunGame.Core.Interface.Base;
|
|
using Milimoe.FunGame.Core.Library.Common.Network;
|
|
using Milimoe.FunGame.Core.Library.Constant;
|
|
using Milimoe.FunGame.Server.Model;
|
|
using Milimoe.FunGame.Server.Utility;
|
|
using Milimoe.FunGame.WebAPI.Architecture;
|
|
using Milimoe.FunGame.WebAPI.Controllers;
|
|
|
|
namespace Milimoe.FunGame.WebAPI.Models
|
|
{
|
|
public class RESTfulAPIModel(ISocketListener<RESTfulAPI> server, string clientip) : ServerModel<RESTfulAPI>(server, new RESTfulAPI(Guid.NewGuid(), clientip, clientip), false)
|
|
{
|
|
public Guid LastRequestID { get; set; } = Guid.Empty;
|
|
public List<SocketObject> ToBeSent { get; set; } = [];
|
|
|
|
public override async Task<bool> Send(SocketMessageType type, params object[] objs)
|
|
{
|
|
if (type == SocketMessageType.Disconnect || type == SocketMessageType.ForceLogout)
|
|
{
|
|
RemoveUser();
|
|
await Close();
|
|
return true;
|
|
}
|
|
if (type != SocketMessageType.HeartBeat)
|
|
{
|
|
SocketObject obj = new(type, Token, objs);
|
|
if (LastRequestID != Guid.Empty)
|
|
{
|
|
return PostDataController.ResultDatas.TryAdd(LastRequestID, obj);
|
|
}
|
|
else
|
|
{
|
|
ToBeSent.Add(obj);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public override async Task<bool> SocketMessageHandler(ISocketMessageProcessor socket, SocketObject obj)
|
|
{
|
|
// 读取收到的消息
|
|
SocketMessageType type = obj.SocketType;
|
|
Guid token = obj.Token;
|
|
string msg = "";
|
|
|
|
// 验证Token
|
|
if (type != SocketMessageType.HeartBeat && token != socket.Token)
|
|
{
|
|
ServerHelper.WriteLine(GetClientName() + " 使用了非法方式传输消息,服务器拒绝回应 -> [" + SocketSet.GetTypeString(type) + "]");
|
|
return false;
|
|
}
|
|
|
|
if (type == SocketMessageType.DataRequest)
|
|
{
|
|
return await DataRequestHandler(obj);
|
|
}
|
|
|
|
if (type == SocketMessageType.GamingRequest)
|
|
{
|
|
return await GamingRequestHandler(obj);
|
|
}
|
|
|
|
if (type == SocketMessageType.Gaming)
|
|
{
|
|
return await GamingMessageHandler(obj);
|
|
}
|
|
|
|
return await Send(type, msg);
|
|
}
|
|
}
|
|
}
|