首次提交
This commit is contained in:
76
Assets/Scripts/Common/Net/Net.cs
Normal file
76
Assets/Scripts/Common/Net/Net.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network;
|
||||
using Fantasy.Network.Interface;
|
||||
using NBC;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class Net : EventDispatcher
|
||||
{
|
||||
public static Session Session { get; private set; }
|
||||
|
||||
public const int HeartbeatInterval = 5000;
|
||||
|
||||
#region 单例
|
||||
|
||||
private static Net _inst;
|
||||
|
||||
public static Net Inst => _inst ??= new Net();
|
||||
|
||||
public Net()
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 连接创建和回调
|
||||
|
||||
public static Session CreateSession(string address)
|
||||
{
|
||||
var session = SessionHelper.CreateSession(Game.Main, address, OnConnectComplete,
|
||||
OnConnectFail,
|
||||
OnConnectDisconnect);
|
||||
|
||||
Session = session;
|
||||
return session;
|
||||
}
|
||||
|
||||
private static void OnConnectComplete()
|
||||
{
|
||||
Log.Info("连接成功");
|
||||
// 心跳
|
||||
Session.AddComponent<SessionHeartbeatComponent>().Start(HeartbeatInterval);
|
||||
}
|
||||
|
||||
private static void OnConnectFail()
|
||||
{
|
||||
Log.Info("连接失败");
|
||||
// SetLoginState(false);
|
||||
}
|
||||
|
||||
private static void OnConnectDisconnect()
|
||||
{
|
||||
Log.Info("连接断开");
|
||||
// SetLoginState(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 消息收发
|
||||
|
||||
public static FTask<IResponse> Call<T>(T request, long routeId = 0) where T : IRequest
|
||||
{
|
||||
// var response = await Session.Call(request, routeId);
|
||||
// response.DispatchMessage();
|
||||
return Session.Call(request, routeId);
|
||||
;
|
||||
}
|
||||
|
||||
public static void Send<T>(T message, uint rpcId = 0, long routeId = 0) where T : IMessage
|
||||
{
|
||||
Session.Send(message, rpcId, routeId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user