using System; using System.Collections.Generic; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using Google.Protobuf; using Plugins.JNGame.Network.Entity; using Plugins.JNGame.Network.Util; using Plugins.JNGame.System; using Plugins.JNGame.Util; namespace Plugins.JNGame.Network.Group { public class JNClientGroup<T> : SystemBase where T : JNClientBase { //创建通知 protected EventDispatcher _event = new(); //客户端组 public List<T> group = new List<T>(); public override async Task OnInit() { await UniTask.NextFrame(); } public override void OnClose() { base.OnClose(); foreach (var client in group) { client.OnClose(); } group.Clear(); } public virtual void AddClient(T client) { group.Add(client); client.SetEvent(_event); client.OnInit(); } public virtual void RemoveClient(T client) { client.SetEvent(new EventDispatcher()); client.OnClose(); group.Remove(client); } //外部监听消息 public void AddListener(int hId,Action<byte[]> listener) { _event.AddListener($"{hId}",listener); } //删除外部监听 public void RemoveListener(int hId,Action<byte[]> listener) { _event.RemoveListener($"{hId}",listener); } //向服务器发送消息 public void Send(int hId,IMessage data = null,Func<T,bool> filter = null) { filter ??= (T client) => true; group.ForEach(child => { if (filter(child)) { child.Send(hId,data); } }); } } }