广播聊天

This commit is contained in:
bob
2025-08-07 17:51:25 +08:00
parent 70bfe43a80
commit e1a4db89ae
19 changed files with 347 additions and 21 deletions

View File

@@ -11,5 +11,6 @@ public sealed class
Caht2C_SendMessageResponse response, Action reply)
{
ChatSceneHelper.Broadcast(chatUnit.Scene, request.Message);
await FTask.CompletedTask;
}
}

View 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;
}
}

View File

@@ -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();
}
}

View 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;
}
}

View 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
}