58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using System.Reflection;
|
|
using AlicizaX.Runtime;
|
|
using Cysharp.Threading.Tasks;
|
|
using Fantasy;
|
|
using Fantasy.Network;
|
|
using Log = AlicizaX.Runtime.Log;
|
|
|
|
namespace AlicizaX.Network.Runtime
|
|
{
|
|
[UnityEngine.Scripting.Preserve]
|
|
internal sealed class NetworkManager : INetworkManager
|
|
{
|
|
private Scene _scene;
|
|
private Session _session;
|
|
private int _heartInterval = 2000;
|
|
private bool isConnect;
|
|
|
|
void IModule.Dispose()
|
|
{
|
|
_scene?.Dispose();
|
|
_session?.Dispose();
|
|
}
|
|
|
|
public async UniTask Initialize(params Assembly[] assemblies)
|
|
{
|
|
await Fantasy.Platform.Unity.Entry.Initialize(assemblies);
|
|
_scene = await Scene.Create(SceneRuntimeType.MainThread);
|
|
}
|
|
|
|
public void Connect(string remoteAddress, NetworkProtocolType networkProtocolType, bool isHttps)
|
|
{
|
|
if (isConnect) return;
|
|
_session = _scene.Connect(remoteAddress, networkProtocolType, OnConnectComplete, OnConnectFail, OnConnectDisconnect, isHttps);
|
|
}
|
|
|
|
private void OnConnectComplete()
|
|
{
|
|
isConnect = true;
|
|
Log.Debug("连接成功");
|
|
// 添加心跳组件给Session。
|
|
// Start(2000)就是2000毫秒。
|
|
_session.AddComponent<SessionHeartbeatComponent>().Start(_heartInterval);
|
|
}
|
|
|
|
private void OnConnectFail()
|
|
{
|
|
isConnect = false;
|
|
Log.Debug("连接失败");
|
|
}
|
|
|
|
private void OnConnectDisconnect()
|
|
{
|
|
isConnect = false;
|
|
Log.Debug("连接断开");
|
|
}
|
|
}
|
|
}
|