聊天频道

This commit is contained in:
2025-08-08 09:13:09 +08:00
parent e1a4db89ae
commit 61496d4616
11 changed files with 190 additions and 6 deletions

View File

@@ -81,8 +81,11 @@ message Game2C_MailState // ICustomRouteMessage,GameRoute
///发送聊天
message C2Chat_SendMessageRequest // ICustomRouteRequest,Caht2C_SendMessageResponse,ChatRoute
{
string Message = 1;
int32 Type = 1; //消息类型
string Message = 2;
int64 Target = 3; //目标id如果有
}
///发送聊天响应
message Caht2C_SendMessageResponse // ICustomRouteResponse
{
@@ -92,5 +95,5 @@ message Caht2C_SendMessageResponse // ICustomRouteResponse
///推送消息
message Chat2C_Message // ICustomRouteMessage,ChatRoute
{
string Message = 1;
ChatMessageInfo Message = 1;
}

View File

@@ -0,0 +1,15 @@
syntax = "proto3";
package Fantasy.Network.Message;
message ChatUserInfo
{
int64 Id = 1;//用户id
int64 Name = 2;//用户名
}
message ChatMessageInfo
{
int32 Type = 1; //消息类型
ChatUserInfo Trigger = 2; //发送者
repeated string Content = 3; //内容
}

View File

@@ -0,0 +1,11 @@
using Fantasy.Entitas;
namespace NB.Chat;
/// <summary>
/// 聊天频道管理
/// </summary>
public class ChatChannelCenterComponent : Entity
{
public readonly Dictionary<long, ChatChannelComponent> Channels = new Dictionary<long, ChatChannelComponent>();
}

View File

@@ -0,0 +1,11 @@
using Fantasy.Entitas;
namespace NB.Chat;
/// <summary>
/// 聊天频道实体
/// </summary>
public class ChatChannelComponent : Entity
{
public readonly HashSet<long> Units = new HashSet<long>();
}

View File

@@ -17,4 +17,47 @@ using Fantasy.Serialize;
namespace Fantasy
{
[ProtoContract]
public partial class ChatUserInfo : AMessage, IProto
{
public static ChatUserInfo Create(Scene scene)
{
return scene.MessagePoolComponent.Rent<ChatUserInfo>();
}
public override void Dispose()
{
Id = default;
Name = default;
#if FANTASY_NET || FANTASY_UNITY
GetScene().MessagePoolComponent.Return<ChatUserInfo>(this);
#endif
}
[ProtoMember(1)]
public long Id { get; set; }
[ProtoMember(2)]
public long Name { get; set; }
}
[ProtoContract]
public partial class ChatMessageInfo : AMessage, IProto
{
public static ChatMessageInfo Create(Scene scene)
{
return scene.MessagePoolComponent.Rent<ChatMessageInfo>();
}
public override void Dispose()
{
Type = default;
Trigger = default;
Content.Clear();
#if FANTASY_NET || FANTASY_UNITY
GetScene().MessagePoolComponent.Return<ChatMessageInfo>(this);
#endif
}
[ProtoMember(1)]
public int Type { get; set; }
[ProtoMember(2)]
public ChatUserInfo Trigger { get; set; }
[ProtoMember(3)]
public List<string> Content = new List<string>();
}
}

View File

@@ -299,7 +299,9 @@ namespace Fantasy
}
public override void Dispose()
{
Type = default;
Message = default;
Target = default;
#if FANTASY_NET || FANTASY_UNITY
GetScene().MessagePoolComponent.Return<C2Chat_SendMessageRequest>(this);
#endif
@@ -310,7 +312,11 @@ namespace Fantasy
[ProtoIgnore]
public int RouteType => Fantasy.RouteType.ChatRoute;
[ProtoMember(1)]
public int Type { get; set; }
[ProtoMember(2)]
public string Message { get; set; }
[ProtoMember(3)]
public long Target { get; set; }
}
/// <summary>
/// 发送聊天响应
@@ -354,6 +360,6 @@ namespace Fantasy
[ProtoIgnore]
public int RouteType => Fantasy.RouteType.ChatRoute;
[ProtoMember(1)]
public string Message { get; set; }
public ChatMessageInfo Message { get; set; }
}
}

View File

@@ -0,0 +1,39 @@
using Fantasy;
namespace NB.Chat;
public static class ChatChannelHelper
{
/// <summary>
/// 申请一个频道
/// </summary>
/// <param name="scene"></param>
/// <param name="channelId"></param>
/// <returns></returns>
public static ChatChannelComponent Apply(Scene scene, long channelId)
{
return scene.GetComponent<ChatChannelCenterComponent>().Apply(channelId);
}
/// <summary>
/// 尝试获取一个频道
/// </summary>
/// <param name="scene"></param>
/// <param name="channelId"></param>
/// <param name="channel"></param>
/// <returns></returns>
public static bool TryGet(Scene scene, long channelId, out ChatChannelComponent? channel)
{
return scene.GetComponent<ChatChannelCenterComponent>().TryGet(channelId, out channel);
}
/// <summary>
/// 解散频道
/// </summary>
/// <param name="scene"></param>
/// <param name="channelId"></param>
public static void Disband(Scene scene, long channelId)
{
scene.GetComponent<ChatChannelCenterComponent>().Disband(channelId);
}
}

View File

@@ -0,0 +1,51 @@
using Fantasy.Entitas;
namespace NB.Chat;
public static class ChatChannelCenterComponentSystem
{
/// <summary>
/// 申请
/// </summary>
/// <param name="self"></param>
/// <param name="channelId"></param>
/// <returns></returns>
public static ChatChannelComponent Apply(this ChatChannelCenterComponent self, long channelId)
{
if (self.Channels.TryGetValue(channelId, out var channel))
{
return channel;
}
channel = Entity.Create<ChatChannelComponent>(self.Scene, channelId, true, true);
self.Channels.Add(channelId, channel);
return channel;
}
/// <summary>
/// 获取
/// </summary>
/// <param name="self"></param>
/// <param name="channelId"></param>
/// <param name="channel"></param>
/// <returns></returns>
public static bool TryGet(this ChatChannelCenterComponent self, long channelId, out ChatChannelComponent? channel)
{
return self.Channels.TryGetValue(channelId, out channel);
}
/// <summary>
/// 解散
/// </summary>
/// <param name="self"></param>
/// <param name="channelId"></param>
public static void Disband(this ChatChannelCenterComponent self, long channelId)
{
if (self.Channels.Remove(channelId, out var channel))
{
return;
}
channel?.Dispose();
}
}

View File

@@ -1,6 +1,9 @@
using Fantasy;
using System.Diagnostics;
using Fantasy;
using Fantasy.Async;
using Fantasy.Network;
using Fantasy.Network.Interface;
using Fantasy.PacketParser;
namespace NB.Gate;
@@ -10,10 +13,11 @@ public class Chat2G_ChatMessageHandler : Route<Scene, Chat2G_ChatMessage>
{
var chatMessage = new Chat2C_Message()
{
Message = message.Message,
Message = new ChatMessageInfo(),
};
var gateUnitManage = scene.GetComponent<GateUnitManageComponent>();
foreach (var session in gateUnitManage.ForEachUnitSession())
{
session.Send(chatMessage);

View File

@@ -42,6 +42,7 @@ public class OnSceneCreate_Init : AsyncEventSystem<OnCreateScene>
{
//用于管理玩家的组件
scene.AddComponent<ChatUnitManageComponent>();
scene.AddComponent<ChatChannelCenterComponent>();
break;
}
}

View File

@@ -73,5 +73,5 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AUInt16_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F8bc9cdb23bc146bcaaae0bb9e45e5d46d9dc00_003F40_003Fbc77e381_003FUInt16_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AUnsafe_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F8bc9cdb23bc146bcaaae0bb9e45e5d46d9dc00_003F0c_003F06784792_003FUnsafe_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AX509Certificate2_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa0c1b406692e44bb91ab90020934356c23c000_003Fa2_003Fbcd53670_003FX509Certificate2_002Ecs_002Fz_003A2_002D1/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">VISIBLE_FILES</s:String>
</wpf:ResourceDictionary>