广播聊天
This commit is contained in:
@@ -11,5 +11,6 @@ public sealed class
|
||||
Caht2C_SendMessageResponse response, Action reply)
|
||||
{
|
||||
ChatSceneHelper.Broadcast(chatUnit.Scene, request.Message);
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
22
Hotfix/Chat/Handler/G2Chat_EnterRequestHandler.cs
Normal file
22
Hotfix/Chat/Handler/G2Chat_EnterRequestHandler.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network.Interface;
|
||||
using NB.Game;
|
||||
|
||||
namespace NB.Chat;
|
||||
|
||||
public class G2Chat_EnterRequestHandler: RouteRPC<Scene, G2Chat_EnterRequest, Chat2G_EnterResponse>
|
||||
{
|
||||
protected override async FTask Run(Scene scene, G2Chat_EnterRequest request, Chat2G_EnterResponse response,
|
||||
Action reply)
|
||||
{
|
||||
Log.Debug($"收到 G2Chat_EnterRequestHandler {request.AccountId}");
|
||||
|
||||
// 在缓存中检查该账号是否存在
|
||||
var chatUnitManageComponent = scene.GetComponent<ChatUnitManageComponent>();
|
||||
var account = await chatUnitManageComponent.Online(scene, request.AccountId, request.GateRouteId);
|
||||
|
||||
response.RoleRouteId = account.RuntimeId;
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Platform.Net;
|
||||
|
||||
namespace NB.Chat;
|
||||
@@ -12,10 +13,11 @@ public static class ChatSceneHelper
|
||||
/// <param name="message"></param>
|
||||
public static void Broadcast(Scene scene, string message)
|
||||
{
|
||||
Log.Info("广播消息===");
|
||||
//发送给所有Gate服务器,让Gate转发给其他客户端
|
||||
var gateConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Gate);
|
||||
|
||||
var sendMessage = new Chat2C_Message()
|
||||
var sendMessage = new Chat2G_ChatMessage()
|
||||
{
|
||||
Message = message
|
||||
};
|
||||
@@ -26,4 +28,34 @@ public static class ChatSceneHelper
|
||||
networkMessagingComponent.SendInnerRoute(config.RouteId, sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static async FTask<long> Online(Scene scene, long accountID, long gateRuntimeId)
|
||||
{
|
||||
var gameSceneConfig = GetSceneConfig();
|
||||
var gameRouteId = gameSceneConfig.RouteId;
|
||||
//连接到游戏中心服
|
||||
var gameResponse = (Chat2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
gameRouteId, new G2Chat_EnterRequest()
|
||||
{
|
||||
AccountId = accountID,
|
||||
GateRouteId = gateRuntimeId
|
||||
});
|
||||
|
||||
if (gameResponse.ErrorCode != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return gameResponse.RoleRouteId;
|
||||
}
|
||||
|
||||
private static SceneConfig GetSceneConfig()
|
||||
{
|
||||
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Chat);
|
||||
|
||||
return gameSceneConfigs.First();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
20
Hotfix/Chat/Helper/ChatUnitFactory.cs
Normal file
20
Hotfix/Chat/Helper/ChatUnitFactory.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Entitas;
|
||||
|
||||
namespace NB.Chat;
|
||||
|
||||
public static class ChatUnitFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建一个新的Player
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="aId">ToKen令牌传递过来的aId</param>
|
||||
/// <param name="isSaveDataBase">是否在创建的过程中保存到数据库</param>
|
||||
/// <returns></returns>
|
||||
public static ChatUnit Create(Scene scene, long aId)
|
||||
{
|
||||
var player = Entity.Create<ChatUnit>(scene, aId, true, true);
|
||||
return player;
|
||||
}
|
||||
}
|
||||
75
Hotfix/Chat/System/ChatUnitManageComponentSystem.cs
Normal file
75
Hotfix/Chat/System/ChatUnitManageComponentSystem.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using NB.Game;
|
||||
|
||||
namespace NB.Chat;
|
||||
|
||||
public static class ChatUnitManageComponentSystem
|
||||
{
|
||||
#region 上线下线
|
||||
|
||||
/// <summary>
|
||||
/// 玩家上线
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="accountId"></param>
|
||||
/// <param name="gateRouteId"></param>
|
||||
public static async FTask<ChatUnit> Online(this ChatUnitManageComponent self, Scene scene, long accountId,
|
||||
long gateRouteId)
|
||||
{
|
||||
if (!self.TryGet(accountId, out var account))
|
||||
{
|
||||
account = ChatUnitFactory.Create(scene, accountId);
|
||||
self.Add(account);
|
||||
}
|
||||
|
||||
account.GateRouteId = gateRouteId;
|
||||
|
||||
await FTask.CompletedTask;
|
||||
|
||||
return account;
|
||||
}
|
||||
|
||||
public static async FTask Offline(this ChatUnitManageComponent self, Scene scene, long accountId)
|
||||
{
|
||||
self.Remove(accountId);
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取&移除
|
||||
|
||||
public static void Add(this ChatUnitManageComponent self, ChatUnit account)
|
||||
{
|
||||
self.ChatUnits.Add(account.Id, account);
|
||||
}
|
||||
|
||||
public static ChatUnit? Get(this ChatUnitManageComponent self, long accountId)
|
||||
{
|
||||
return self.ChatUnits.GetValueOrDefault(accountId);
|
||||
}
|
||||
|
||||
public static bool TryGet(this ChatUnitManageComponent self, long accountId, out ChatUnit? account)
|
||||
{
|
||||
return self.ChatUnits.TryGetValue(accountId, out account);
|
||||
}
|
||||
|
||||
public static void Remove(this ChatUnitManageComponent self, long accountId, bool isDispose = true)
|
||||
{
|
||||
if (!self.ChatUnits.Remove(accountId, out var account))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isDispose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
account.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Net;
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network;
|
||||
using Fantasy.Platform.Net;
|
||||
|
||||
@@ -7,10 +8,30 @@ namespace NB.Game;
|
||||
|
||||
public static class GameSceneHelper
|
||||
{
|
||||
public static SceneConfig GetSceneConfig(Session session)
|
||||
private static SceneConfig GetSceneConfig()
|
||||
{
|
||||
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Game);
|
||||
|
||||
return gameSceneConfigs.First();
|
||||
}
|
||||
|
||||
public static async FTask<long> Online(Scene scene, long accountID, long gateRuntimeId)
|
||||
{
|
||||
var gameSceneConfig = GetSceneConfig();
|
||||
var gameRouteId = gameSceneConfig.RouteId;
|
||||
//连接到游戏中心服
|
||||
var gameResponse = (Game2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
gameRouteId, new G2Game_EnterRequest()
|
||||
{
|
||||
AccountId = accountID,
|
||||
GateRouteId = gateRuntimeId
|
||||
});
|
||||
|
||||
if (gameResponse.ErrorCode != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return gameResponse.RoleRouteId;
|
||||
}
|
||||
}
|
||||
24
Hotfix/Gate/Handler/Inner/Chat2G_ChatMessageHandler.cs
Normal file
24
Hotfix/Gate/Handler/Inner/Chat2G_ChatMessageHandler.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network.Interface;
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public class Chat2G_ChatMessageHandler : Route<Scene, Chat2G_ChatMessage>
|
||||
{
|
||||
protected override async FTask Run(Scene scene, Chat2G_ChatMessage message)
|
||||
{
|
||||
var chatMessage = new Chat2C_Message()
|
||||
{
|
||||
Message = message.Message,
|
||||
};
|
||||
|
||||
var gateUnitManage = scene.GetComponent<GateUnitManageComponent>();
|
||||
foreach (var session in gateUnitManage.ForEachUnitSession())
|
||||
{
|
||||
session.Send(chatMessage);
|
||||
}
|
||||
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,18 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network;
|
||||
using NB.Chat;
|
||||
using NB.Game;
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public static class GateLoginHelper
|
||||
{
|
||||
#region 上线
|
||||
|
||||
/// <summary>
|
||||
/// 网关通知其他服务器上线
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
/// <param name="gateUnit"></param>
|
||||
public static async FTask<uint> Online(GateUnit gateUnit)
|
||||
{
|
||||
@@ -39,29 +41,34 @@ public static class GateLoginHelper
|
||||
|
||||
gateUnitSessionComponent.AccountID = gateUnit.AccountID;
|
||||
|
||||
//安排服务器,并通知进入
|
||||
var gameSceneConfig = GameSceneHelper.GetSceneConfig(session);
|
||||
var gameRouteId = gameSceneConfig.RouteId;
|
||||
//连接到游戏中心服
|
||||
var gameResponse = (Game2G_EnterResponse)await session.Scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
gameRouteId, new G2Game_EnterRequest()
|
||||
{
|
||||
AccountId = gateUnit.AccountID,
|
||||
GateRouteId = session.RuntimeId
|
||||
});
|
||||
|
||||
if (gameResponse.ErrorCode != 0)
|
||||
//安排游戏服务器,并通知进入
|
||||
var gameRouteId = await GameSceneHelper.Online(session.Scene, gateUnit.AccountID, session.RuntimeId);
|
||||
if (gameRouteId <= 0)
|
||||
{
|
||||
return ErrorCode.OnlineSceneFailed;
|
||||
}
|
||||
|
||||
routeComponent.AddAddress(RouteType.GameRoute, gameResponse.RoleRouteId);
|
||||
Log.Info($"连接游戏服成功,gameRouteId:{gameRouteId}");
|
||||
routeComponent.AddAddress(RouteType.GameRoute, gameRouteId);
|
||||
gateUnit.GameSceneRouteId = gameRouteId;
|
||||
|
||||
|
||||
|
||||
// //安排进入的聊天服
|
||||
var chatRouteId = await ChatSceneHelper.Online(session.Scene, gateUnit.AccountID, session.RuntimeId);
|
||||
if (chatRouteId <= 0)
|
||||
{
|
||||
return ErrorCode.OnlineSceneFailed;
|
||||
}
|
||||
routeComponent.AddAddress(RouteType.ChatRoute, chatRouteId);
|
||||
gateUnit.ChatSceneRouteId = chatRouteId;
|
||||
Log.Info($"连接聊天服成功,gameRouteId:{gameRouteId}");
|
||||
return ErrorCode.Successful;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 下线
|
||||
|
||||
/// <summary>
|
||||
/// 网关通知其他服务器下线
|
||||
/// </summary>
|
||||
@@ -69,7 +76,11 @@ public static class GateLoginHelper
|
||||
/// <param name="gateUnit"></param>
|
||||
public static async FTask<uint> Offline(GateUnit gateUnit)
|
||||
{
|
||||
await FTask.CompletedTask;
|
||||
//通知服务器下线
|
||||
return ErrorCode.Successful;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
@@ -52,4 +52,19 @@ public static class GateUnitManageComponentSystem
|
||||
unit.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回话迭代器
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Session> ForEachUnitSession(this GateUnitManageComponent self)
|
||||
{
|
||||
foreach (var (_, gateUnit) in self.Units)
|
||||
{
|
||||
Session gateSession = gateUnit.Session;
|
||||
if (gateSession == null) continue;
|
||||
yield return gateSession;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,8 +28,8 @@ public static class GateJWTComponentSystem
|
||||
ValidateIssuer = true, // 验证发行者
|
||||
ValidateAudience = true, // 验证受众
|
||||
ValidateIssuerSigningKey = true, // 验证签名密钥
|
||||
ValidIssuer = "Fantasy", // 有效的发行者
|
||||
ValidAudience = "Fantasy", // 有效的受众
|
||||
ValidIssuer = "NoBug", // 有效的发行者
|
||||
ValidAudience = "NoBug", // 有效的受众
|
||||
IssuerSigningKey = new RsaSecurityKey(rsa) // RSA公钥作为签名密钥
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Event;
|
||||
using NB.Authentication;
|
||||
using NB.Chat;
|
||||
using NB.Game;
|
||||
using NB.Gate;
|
||||
|
||||
@@ -37,6 +38,12 @@ public class OnSceneCreate_Init : AsyncEventSystem<OnCreateScene>
|
||||
scene.AddComponent<PlayerManageComponent>();
|
||||
break;
|
||||
}
|
||||
case SceneType.Chat:
|
||||
{
|
||||
//用于管理玩家的组件
|
||||
scene.AddComponent<ChatUnitManageComponent>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
await FTask.CompletedTask;
|
||||
|
||||
Reference in New Issue
Block a user