HaoYueNet/Simple/ClientCore/Network/NetworkHelper.cs

77 lines
2.1 KiB
C#
Raw Normal View History

2023-05-25 13:46:54 +08:00
using AxibugProtobuf;
using HaoYueNet.ClientNetwork;
2021-12-10 23:38:40 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-05-25 13:46:54 +08:00
namespace ClientCore.Network
2021-12-10 23:38:40 +08:00
{
/// <summary>
/// 继承网络库,以支持网络功能
/// </summary>
public class NetworkHelper : NetworkHelperCore
{
public NetworkHelper()
{
//指定接收服务器数据事件
OnDataCallBack += GetDataCallBack;
//断开连接
2021-12-13 23:53:01 +08:00
OnClose += OnConnectClose;
2021-12-10 23:38:40 +08:00
//网络库调试信息输出事件,用于打印连接断开,收发事件
OnLogOut += NetworkDeBugLog;
OnConnected += NetworkConnected;
}
public void NetworkConnected(bool IsConnect)
{
if (IsConnect)
NetworkDeBugLog("服务器连接成功");
else
{
NetworkDeBugLog("服务器连接失败");
//to do 重连逻辑
}
}
public void NetworkDeBugLog(string str)
{
//用于Unity内的输出
//Debug.Log("NetCoreDebug >> "+str);
Console.WriteLine("NetCoreDebug >> " + str);
}
/// <summary>
/// 接受包回调
/// </summary>
/// <param name="CMDID">协议ID</param>
/// <param name="ERRCODE">错误编号</param>
/// <param name="data">业务数据</param>
public void GetDataCallBack(int CMDID, int ERRCODE, byte[] data)
{
2021-12-13 23:53:01 +08:00
NetworkDeBugLog("收到消息 CMDID =>" + CMDID + " ERRCODE =>" + ERRCODE + " 数据长度=>" + data.Length);
2021-12-10 23:38:40 +08:00
try
{
2023-05-25 18:04:01 +08:00
//抛出网络数据
NetMsg.Instance.PostNetMsgEvent(CMDID, data);
2021-12-10 23:38:40 +08:00
}
catch (Exception ex)
{
2021-12-13 23:53:01 +08:00
NetworkDeBugLog("逻辑处理错误:" + ex.ToString());
2021-12-10 23:38:40 +08:00
}
}
/// <summary>
/// 关闭连接
/// </summary>
2021-12-13 23:53:01 +08:00
public void OnConnectClose()
2021-12-10 23:38:40 +08:00
{
2021-12-13 23:53:01 +08:00
NetworkDeBugLog("OnConnectClose");
2021-12-10 23:38:40 +08:00
}
}
}