注意:已弃用 UNet,未来会将其从 Unity 中删除。一个新系统正在开发中。如需了解更多信息和后续步骤,请参阅此博客文章以及常见问题解答 (FAQ)。 |
NetworkConnection 是一个高级 API 类,可用于封装网络连接。(NetworkClient)[class-NetworkClient] 对象具有一个 NetworkConnection
,而 NetworkServer 具有多个连接:与每个客户端有一个连接。NetworkConnection 能够作为网络消息来发送字节数组或序列化对象。
属性: | 功能: | |
---|---|---|
hostId | 此连接的 NetworkTransport hostId。 | |
connectionId | 此连接的 NetworkTransport connectionId。 |
|
isReady | 该标志用于控制是否将状态更新发送到此连接 | |
lastMessageTime | 在此连接上收到消息的最后时间。 | |
address | 此连接所连接到的端点的 IP 地址。 | |
playerControllers | 已使用 AddPlayer() 添加的玩家的集合。 | |
clientOwnedObjects | 此连接有授权的对象的集合。 |
NetworkConnection 类包含当数据发送到传输层或者从传输层接收数据时调用的虚拟函数。这些函数允许专用版本的 NetworkConnection 检查或修改此数据,甚至将数据路由到不同源。下面显示了这些函数,包括默认行为:
public virtual void TransportRecieve(byte[] bytes, int numBytes, int channelId)
{
HandleBytes(bytes, numBytes, channelId);
}
public virtual bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error)
{
return NetworkTransport.Send(hostId, connectionId, channelId, bytes, numBytes, out error);
}
这些函数的一个用例是记录传入和传出数据包的内容。以下是派生自 NetworkConnection 的 DebugConnection 类的示例,此类用于将数据包的前 50 字节记录到控制台。要使用这样的类,请在 NetworkClient 或 NetworkServer 上调用 SetNetworkConnectionClass() 函数。
class DebugConnection : NetworkConnection
{
public override void TransportRecieve(byte[] bytes, int numBytes, int channelId)
{
StringBuilder msg = new StringBuilder();
for (int i = 0; i < numBytes; i++)
{
var s = String.Format("{0:X2}", bytes[i]);
msg.Append(s);
if (i > 50) break;
}
UnityEngine.Debug.Log("TransportRecieve h:" + hostId + " con:" + connectionId + " bytes:" + numBytes + " " + msg);
HandleBytes(bytes, numBytes, channelId);
}
public override bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error)
{
StringBuilder msg = new StringBuilder();
for (int i = 0; i < numBytes; i++)
{
var s = String.Format("{0:X2}", bytes[i]);
msg.Append(s);
if (i > 50) break;
}
UnityEngine.Debug.Log("TransportSend h:" + hostId + " con:" + connectionId + " bytes:" + numBytes + " " + msg);
return NetworkTransport.Send(hostId, connectionId, channelId, bytes, numBytes, out error);
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.