Version: 2017.3
public void TransportReceive (byte[] bytes, int numBytes, int channelId);

参数

bytes 收到的数据。
numBytes 收到的数据大小。
channelId 接收数据的通道。

描述

此虚拟函数允许自定义网络连接类在网络数据传递到应用程序之前对这些数据进行处理。

此函数的默认实现是针对接收的数据调用 HandleBytes()。自定义实现也可以使用 HandleBytes(),但可以传递所接收数据的修改版本或其他数据。

此示例将接收的数据记录到控制台,然后将其传递到 HandleBytes。

using UnityEngine;
using UnityEngine.Networking;
using System;
using System.Text;

public class DebugConnection : NetworkConnection { public override void TransportReceive(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.LogError("TransportReceive h:" + hostId + " con:" + connectionId + " bytes:" + numBytes + " " + msg);

HandleBytes(bytes, numBytes, channelId); } }

此功能的其他用途还包括数据压缩或数据加密。

通过设置 NetworkServer.NetworkConnectionClass 和 NetworkClient.NetworkConnectionClass 来使用的自定义网络连接类。

using UnityEngine;
using UnityEngine.Networking;

public class SpaceManager : NetworkManager { void Start() { NetworkServer.networkConnectionClass = typeof(DebugConnection); NetworkClient.networkConnectionClass = typeof(DebugConnection); } }