模块介绍
Coding Style wiki
基本说明
网络模块,网络事件分为 OnOpen, OnBinary, OnMessage, OnError, OnClose,也实现多节点 (NetNode),另外可以设置心跳检测回调、超时处理回调、断线重连回调,并且也能实现 INetTips 网络消息接口的实现。
应用说明
- NetManager (网络节点管理器)
- NetNode (网络节点)
- INetProvider (网络供应者)
- TcpNetProvider (TCP)
- KcpNetProvider (KCP)
- WebSocketNetProvider (WebSocket)
- NetOption (连接配置)
- TcpNetOption
- KcpNetOption
- WebSocketNetOption
- INetTips (网络状态提示接口)
注意 已提供以下 NetProvider (可自行扩展)。
- TCP (Telepathy TCP)
- 注意:Telepathy 框架已处理分黏包问题,会强制在 Packet 前面加入 4 bytes 來表示封包长度。
- KCP (kcp2k)
- WebSocket (UnityWebSocket)
示例使用 ioGame 作为服务端 (高效、方便快速切换连接方式),客户端使用 NetFrame - TcpNetProvider, WebSocketNetProvider 作为连接:
| ioGame Server (TCP) | NetFrame - TcpNetProvider Client |
|---|---|
![]() | ![]() |
![]() | ![]() |
| ioGame Server (WebSocket) | NetFrame - WebSocketNetProvider Client |
|---|---|
![]() | ![]() |
简单使用
自行定义 NetNode ID
public enum NNID
{
WebSocket = 0,
TCP = 1,
KCP = 2
}
开始初始 NetNode
/// <summary>
/// Init WebSocket net node
/// </summary>
public static void InitWebSocketNetNode()
{
var netTips = new NetTipsExample();
NetNode netNode = null;
#region WebSocket NetNode Example
netNode = new NetNode(new WebSocketNetProvider(), netTips);
// Set data receive callback
netNode.SetResponseBinaryHandler((recvData) =>
{
Debug.Log("Recv Binary Data (WebSocket)");
});
// Set connecting callback (Before connection)
netNode.SetConnectingHandler(() =>
{
/**
* If there is first verification can do somethings in here
*/
Debug.Log("Process Connecting Event (WebSocket)");
});
// Set connected callback (After connection)
netNode.SetConnectedHandler(() =>
{
/**
* Connection established successfully
*/
Debug.Log("Process Connected Event (WebSocket)");
});
// Set heart beat callback
netNode.SetHeartBeatTickerTime(10f);
netNode.SetHeartBeatAction(() =>
{
/* Process Heart Beat */
});
// Set out receive callback
netNode.SetOutReceiveTickerTime(60f);
netNode.SetOutReceiveAction(() =>
{
/* Process Out Of Receive */
});
// Set reconnect callback
netNode.SetReconnectTickerTime(5f);
netNode.SetReconnectAction(() =>
{
/* Process Reconnect */
});
// Add net node (register)
NetFrames.AddNetNode(netNode, (int)NNID.WebSocket);
#endregion
}
/// <summary>
/// Init TCP net node
/// </summary>
public static void InitTCPNetNode()
{
var netTips = new NetTipsExample();
NetNode netNode = null;
#region TCP NetNode Example
netNode = new NetNode(new TcpNetProvider(), netTips);
// Set data receive callback
netNode.SetResponseBinaryHandler((recvData) =>
{
Debug.Log("Recv Binary Data (TCP)");
});
// Set connecting callback (Before connection)
netNode.SetConnectingHandler(() =>
{
/**
* If there is first verification can do somethings in here
*/
Debug.Log("Process Connecting Event (TCP)");
});
// Set connected callback (After connection)
netNode.SetConnectedHandler(() =>
{
/**
* Connection established successfully
*/
Debug.Log("Process Connected Event (TCP)");
});
// Set heart beat callback
netNode.SetHeartBeatTickerTime(10f);
netNode.SetHeartBeatAction(() =>
{
/* Process Heart Beat */
});
// Set out receive callback
netNode.SetOutReceiveTickerTime(60f);
netNode.SetOutReceiveAction(() =>
{
/* Process Out Of Receive */
});
// Set reconnect callback
netNode.SetReconnectTickerTime(5f);
netNode.SetReconnectAction(() =>
{
/* Process Reconnect */
});
// Add net node (register)
NetFrames.AddNetNode(netNode, (int)NNID.TCP);
#endregion
}
/// <summary>
/// Init KCP net node
/// </summary>
public static void InitKCPNetNode()
{
var netTips = new NetTipsExample();
NetNode netNode = null;
#region KCP NetNode Example
netNode = new NetNode(new KcpNetProvider(), netTips);
// Set data receive callback
netNode.SetResponseBinaryHandler((recvData) =>
{
Debug.Log("Recv Binary Data (KCP)");
});
// Set connecting callback (Before connection)
netNode.SetConnectingHandler(() =>
{
/**
* If there is first verification can do somethings in here
*/
Debug.Log("Process Connecting Event (KCP)");
});
// Set connected callback (After connection)
netNode.SetConnectedHandler(() =>
{
/**
* Connection established successfully
*/
Debug.Log("Process Connected Event (KCP)");
});
// Set heart beat callback
netNode.SetHeartBeatTickerTime(10f);
netNode.SetHeartBeatAction(() =>
{
/* Process Heart Beat */
});
// Set out receive callback
netNode.SetOutReceiveTickerTime(60f);
netNode.SetOutReceiveAction(() =>
{
/* Process Out Of Receive */
});
// Set reconnect callback
netNode.SetReconnectTickerTime(5f);
netNode.SetReconnectAction(() =>
{
/* Process Reconnect */
});
// Add net node (register)
NetFrames.AddNetNode(netNode, (int)NNID.KCP);
#endregion
}
创建 NetNode 连接
/// <summary>
/// Create connection
/// </summary>
/// <param name="netOption"></param>
/// <param name="nnid"></param>
public static void OpenConnection(NetOption netOption, int nnid = 0)
{
// Init net node if not exist
switch (nnid)
{
case (int)NNID.WebSocket:
if (NetFrames.GetNetNode((int)NNID.WebSocket) == null)
InitWebSocketNetNode();
break;
case (int)NNID.TCP:
if (NetFrames.GetNetNode((int)NNID.TCP) == null)
InitTCPNetNode();
break;
case (int)NNID.KCP:
if (NetFrames.GetNetNode((int)NNID.KCP) == null)
InitKCPNetNode();
break;
}
// Connect to server
NetFrames.Connect(netOption, nnid);
}
关闭 NetNode 连接
/// <summary>
/// Close connection
/// </summary>
/// <param name="nnid"></param>
public static void CloseConnection(int nnid = 0)
{
// Close connection and remove net node
NetFrames.Close(nnid, true);
}
检查 NetNode 是否已连接
/// <summary>
/// Return connection status
/// </summary>
/// <param name="nnid"></param>
/// <returns></returns>
public static bool IsConnected(int nnid = 0)
{
return NetFrames.IsConnected(nnid);
}
发送 NetNode 数据
/// <summary>
/// Send binary data
/// </summary>
/// <param name="buffer"></param>
/// <param name="nnid"></param>
/// <returns></returns>
public static bool SendData(byte[] buffer, int nnid = 0)
{
// Also you can get NetProvider to send data like:
// NetFrames.GetNetNode((int)NNID.KCP).GetNetProvider<KcpNetProvider>().SendBinary(buffer);
// NetFrames.GetNetNode((int)NNID.KCP).GetNetProvider<KcpNetProvider>().SendBinary(kcp2k.KcpChannel.Unreliable, buffer);
return NetFrames.Send(buffer, nnid);
}





