Important: UNet is a deprecated solution, and a new Multiplayer and Networking Solution (Netcode for GameObjects) is under development. For more information and next steps see the information on the Unity Netcode for GameObjects website. |
NetworkConnection is a high-level API class that encapsulates a network connection. NetworkClient objects have a NetworkConnection
, and NetworkServers have multiple connections - one from each client. NetworkConnections have the ability to send byte arrays, or serialized objects as network messages.
属性: | 功能: | |
---|---|---|
hostId | The NetworkTransport hostId for this connection. | |
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);
}
}
NetworkConnection
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.