mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交Unity 联机Pro
This commit is contained in:
76
JNFrame2/Assets/JNGame/Network/Group/JNClientGroup.cs
Normal file
76
JNFrame2/Assets/JNGame/Network/Group/JNClientGroup.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 285c1ebc46554e9aa51b6831fc09a650
|
||||
timeCreated: 1723170178
|
Reference in New Issue
Block a user