- 利用Http请求实现数据保存
[HttpHandler(SceneType.Account, "/save")]
public class HttpSaveDataRequestHandler: IHttpHandler
{
public async ETTask Handle(Scene scene, HttpListenerContext context)
{
var set = new HashSet<string>() { "Cache", "Chat", "Map", "Rank" };
foreach (StartSceneConfig config in StartSceneConfigCategory.Instance.GetAll().Values)
{
if (set.Contains(config.Name))
{
IResponse resp = null;
try
{
resp = await scene.GetComponent<MessageSender>().Call(config.ActorId, W2Other_SaveDataRequest.Create());
}
catch (Exception)
{
HttpHelper.Response(context, $"保存数据错误: {resp.Error}, {config.Name} - {config.Id}");
return;
}
}
}
HttpHelper.Response(context, "Success");
await ETTask.CompletedTask;
}
}
- 利用Http请求实现数据查询
[HttpHandler(SceneType.Account, "/query_info")]
public class HttpQueryInfoHandler: IHttpHandler
{
public async ETTask Handle(Scene scene, HttpListenerContext context)
{
string sceneName = context.Request.QueryString["scene"];
if (sceneName.IsNullOrEmpty())
{
HttpHelper.Response(context, "scene type is null");
return;
}
string componentName = context.Request.QueryString["component"];
if (componentName.IsNullOrEmpty())
{
HttpHelper.Response(context, "component name is null");
return;
}
var actorId = StartSceneConfigCategory.Instance.GetBySceneName(scene.Zone(), sceneName).ActorId;
var rep = await scene.GetComponent<MessageSender>().Call(actorId, new ConmponentQueryRequest() { ComponentName = componentName });
if (rep == null)
{
HttpHelper.Response(context, "component not found");
return;
}
byte[] data = (rep as ComponentQueryResponse).Entity;
var entity = MongoHelper.Deserialize<Entity>(data);
string field = context.Request.QueryString["field"];
if (!field.IsNullOrEmpty())
{
var f = entity.GetType().GetField(field,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.NonPublic);
if (f == null)
{
HttpHelper.Response(context, "field not found");
return;
}
HttpHelper.Response(context, f.GetValue(entity));
return;
}
HttpHelper.Response(context, entity);
}
}
本项目中使用了如下插件(仅供学习交流使用,请务必支持正版!)
交流群: 951617541