模塊介紹
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);
}





