using System; using Cysharp.Threading.Tasks; using GameFramework; using GameFramework.Network; using UnityGameFramework.Runtime; namespace UnityGameFramework.Runtime.AsyncTask { /// /// Network 异步扩展方法 /// public static class NetworkAsyncExtension { /// /// 异步连接网络 /// /// 网络组件 /// 网络频道名称 /// 连接地址 /// 用户自定义数据 /// 超时时间(秒),0表示不超时 /// 连接成功事件 public static UniTask ConnectAsync(this NetworkComponent networkComponent, string networkChannelName, string address, object userData = null, float timeout = 30f) { return AsyncTaskHelper.WaitEventAsync( NetworkConnectedEventArgs.EventId, args => args.NetworkChannel.Name == networkChannelName, timeout ); } /// /// 异步连接网络(带端口) /// /// 网络组件 /// 网络频道名称 /// 连接地址 /// 端口号 /// 用户自定义数据 /// 超时时间(秒),0表示不超时 /// 连接成功事件 public static UniTask ConnectAsync(this NetworkComponent networkComponent, string networkChannelName, string address, int port, object userData = null, float timeout = 30f) { return ConnectAsync(networkComponent, networkChannelName, $"{address}:{port}", userData, timeout); } /// /// 异步断开网络连接 /// /// 网络组件 /// 网络频道名称 /// 用户自定义数据 /// 超时时间(秒),0表示不超时 /// 关闭事件 public static UniTask DisconnectAsync(this NetworkComponent networkComponent, string networkChannelName, object userData = null, float timeout = 30f) { return AsyncTaskHelper.WaitEventAsync( NetworkClosedEventArgs.EventId, args => args.NetworkChannel.Name == networkChannelName, timeout ); } } }