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 : SystemBase where T : JNClientBase { //创建通知 protected EventDispatcher _event = new(); //客户端组 public List group = new List(); 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 listener) { _event.AddListener($"{hId}",listener); } //删除外部监听 public void RemoveListener(int hId,Action listener) { _event.RemoveListener($"{hId}",listener); } //向服务器发送消息 public void Send(int hId,IMessage data = null,Func filter = null) { filter ??= (T client) => true; group.ForEach(child => { if (filter(child)) { child.Send(hId,data); } }); } } }