chat
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
// using Fantasy;
|
||||
// using Fantasy.Async;
|
||||
// using Fantasy.Network;
|
||||
// using Fantasy.Network.Interface;
|
||||
//
|
||||
// namespace NB.Gate;
|
||||
//
|
||||
// public sealed class C2G_GetAccountInfoRequestHandler : MessageRPC<C2G_GetAccountInfoRequest, G2C_GetAccountInfoResponse>
|
||||
// {
|
||||
// protected override async FTask Run(Session session, C2G_GetAccountInfoRequest request, G2C_GetAccountInfoResponse response, Action reply)
|
||||
// {
|
||||
// var gameAccountFlagComponent = session.GetComponent<PlayerFlagComponent>();
|
||||
//
|
||||
// if (gameAccountFlagComponent == null)
|
||||
// {
|
||||
// // 表示不应该访问这个接口,要先访问登录的接口。
|
||||
// // response.ErrorCode = 1;
|
||||
// session.Dispose();
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// Player account = gameAccountFlagComponent.Player;
|
||||
//
|
||||
// if (account == null)
|
||||
// {
|
||||
// // 表示这个Account已经被销毁过了。不是咱们想要的了
|
||||
// }
|
||||
//
|
||||
// response.GameAccountInfo = account.GetGameAccountInfo();
|
||||
// await FTask.CompletedTask;
|
||||
// }
|
||||
// }
|
||||
@@ -3,6 +3,7 @@ using Fantasy.Async;
|
||||
using Fantasy.Network;
|
||||
using Fantasy.Network.Interface;
|
||||
using Fantasy.Platform.Net;
|
||||
using NB.Game;
|
||||
|
||||
namespace NB.Gate.Handler;
|
||||
|
||||
@@ -13,14 +14,11 @@ public sealed class C2G_LoginRequestHandler : MessageRPC<C2G_LoginRequest, G2C_L
|
||||
{
|
||||
if (string.IsNullOrEmpty(request.ToKen))
|
||||
{
|
||||
// 1、客户端漏传了 response.ErrorCode = 1;
|
||||
// 2、恶意攻击导致的 session.Dispose();
|
||||
session.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
var scene = session.Scene;
|
||||
// Log.Info($"网关服场景 {scene.Id} {scene.RouteId} {scene.SceneConfigId} {scene.RouteId} {session.RouteId}");
|
||||
if (!GateJWTHelper.ValidateToken(scene, request.ToKen, out var accountId))
|
||||
{
|
||||
// 如果失败,表示肯定是恶意攻击、所以毫不犹疑,直接断开当前会话。
|
||||
@@ -28,51 +26,51 @@ public sealed class C2G_LoginRequestHandler : MessageRPC<C2G_LoginRequest, G2C_L
|
||||
return;
|
||||
}
|
||||
|
||||
// 首先需要找到一个需要建立Route的Scene的SceneConfig。
|
||||
// 例子演示的连接的ChatScene,所以这里我通过SceneConfigData拿到这个SceneConfig。
|
||||
// 如果是其他Scene,用法跟这个没有任何区别。
|
||||
var gameSceneConfig = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Game)[0];
|
||||
// 通过chatSceneConfig拿到这个Scene的RouteId
|
||||
var gameRouteId = gameSceneConfig.RouteId;
|
||||
//连接到游戏中心服
|
||||
var gameResponse = (Game2G_EnterResponse)await session.Scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
gameRouteId, new G2Game_EnterRequest()
|
||||
{
|
||||
AccountId = accountId,
|
||||
GateRouteId = session.RuntimeId
|
||||
});
|
||||
|
||||
if (gameResponse.ErrorCode != 0)
|
||||
// 在缓存中检查该账号是否存在
|
||||
var gateUnitManageComponent = scene.GetComponent<GateUnitManageComponent>();
|
||||
|
||||
if (!gateUnitManageComponent.TryGet(accountId, out var gateUnit))
|
||||
{
|
||||
// 如果ErrorCode不是0表示请求的协议发生错误,应该提示给客户端。
|
||||
// 这里就不做这个了。
|
||||
response.ErrorCode = gameResponse.ErrorCode;
|
||||
return;
|
||||
gateUnit = gateUnitManageComponent.Add(session, accountId);
|
||||
}
|
||||
|
||||
// 要实现Route协议的转发,需要给Session添加一个RouteComponent,这个非常重要。
|
||||
var routeComponent = session.AddComponent<RouteComponent>();
|
||||
// 需要再Examples/Config/NetworkProtocol/RouteType.Config里添加一个ChatRoute
|
||||
// 然后点击导表工具,会自动生成一个RouteType.cs文件。
|
||||
// 使用你定义的ChatRoute当routeType的参数传递进去。
|
||||
// routeResponse会返回一个ChatRouteId,这个就是Chat的RouteId。
|
||||
routeComponent.AddAddress(RouteType.GameRoute, gameResponse.RoleRouteId);
|
||||
// 这些操作完成后,就完成了Route消息的建立。
|
||||
// 后面可以直接发送Route消息通过Gate自动中转给Chat了。
|
||||
if (gateUnit == null)
|
||||
{
|
||||
Log.Error("创建GateUnit失败");
|
||||
session.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 给当前Session添加一个组件,当Session销毁的时候会销毁这个组件。
|
||||
var accountFlagComponent = session.AddComponent<SessionPlayerComponent>();
|
||||
accountFlagComponent.AccountID = accountId;
|
||||
|
||||
// account.SessionRunTimeId = session.RuntimeId;
|
||||
// response.GameAccountInfo = account.GetGameAccountInfo();
|
||||
|
||||
response.ErrorCode = await GateLoginHelper.Online(gateUnit);
|
||||
Log.Debug($"当前的Gate服务器:{session.Scene.SceneConfigId} accountId:{accountId}");
|
||||
|
||||
|
||||
|
||||
Log.Debug($"网关内网连接到游戏服成功 routerId={gameResponse.RoleRouteId}");
|
||||
// var gameSceneConfig = GameSceneHelper.GetSceneConfig(session);
|
||||
//
|
||||
// // 通过chatSceneConfig拿到这个Scene的RouteId
|
||||
// var gameRouteId = gameSceneConfig.RouteId;
|
||||
// //连接到游戏中心服
|
||||
// var gameResponse = (Game2G_EnterResponse)await session.Scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
// gameRouteId, new G2Game_EnterRequest()
|
||||
// {
|
||||
// AccountId = accountId,
|
||||
// GateRouteId = session.RuntimeId
|
||||
// });
|
||||
//
|
||||
// if (gameResponse.ErrorCode != 0)
|
||||
// {
|
||||
// // 如果ErrorCode不是0表示请求的协议发生错误,应该提示给客户端。
|
||||
// response.ErrorCode = gameResponse.ErrorCode;
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // 要实现Route协议的转发,需要给Session添加一个RouteComponent,这个非常重要。
|
||||
// var routeComponent = session.AddComponent<RouteComponent>();
|
||||
// routeComponent.AddAddress(RouteType.GameRoute, gameResponse.RoleRouteId);
|
||||
//
|
||||
// // 给当前Session添加一个组件,当Session销毁的时候会销毁这个组件。
|
||||
// var accountFlagComponent = session.AddComponent<SessionPlayerComponent>();
|
||||
// accountFlagComponent.AccountID = accountId;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user