Version: 2017.3
public static Networking.NetworkEventType Receive (out int hostId, out int connectionId, out int channelId, byte[] buffer, int bufferSize, out int receivedSize, out byte error);

参数

hostId 与事件关联的主机 ID。
connectionId 收到事件的 connectionID。
channelId 与事件关联的通道 ID。
buffer 用于保存所接收数据的缓冲区。
bufferSize 提供的缓冲区的大小。
receivedSize 实际收到的数据大小。
error 错误(可以转换为 NetworkError 以便了解更多信息)。

返回

NetworkEventType 返回的事件的类型。

描述

被调用以便轮询底层系统中的事件。

using UnityEngine;
using UnityEngine.Networking;

public class ConnectExample : MonoBehaviour { int connectionId; int channelId; int hostId;

void Start() { // Init Transport using default values. NetworkTransport.Init();

// Create a connection config and add a Channel. ConnectionConfig config = new ConnectionConfig(); channelId = config.AddChannel(QosType.Reliable);

// Create a topology based on the connection config. HostTopology topology = new HostTopology(config, 10);

// Create a host based on the topology we just created, and bind the socket to port 12345. hostId = NetworkTransport.AddHost(topology, 12345);

// Connect to the host with IP 10.0.0.42 and port 54321 byte error; connectionId = NetworkTransport.Connect(hostId, "10.0.0.42", 54321, 0, out error); }

void Update() { int outHostId; int outConnectionId; int outChannelId;

int receivedSize; byte error; byte[] buffer = new byte[256]; NetworkEventType evt = NetworkTransport.Receive(out outHostId, out outConnectionId, out outChannelId, buffer, buffer.Length, out receivedSize, out error);

switch (evt) { case NetworkEventType.ConnectEvent: { OnConnect(outHostId, outConnectionId, (NetworkError)error); break; } case NetworkEventType.DisconnectEvent: { OnDisconnect(outHostId, outConnectionId, (NetworkError)error); break; } case NetworkEventType.DataEvent: { OnData(outHostId, outConnectionId, outChannelId, buffer, receivedSize, (NetworkError)error); break; } case NetworkEventType.BroadcastEvent: { OnBroadcast(outHostId, buffer, receivedSize, (NetworkError)error); break; } case NetworkEventType.Nothing: break;

default: Debug.LogError("Unknown network message type received: " + evt); break; } }

void OnConnect(int hostId, int connectionId, NetworkError error) { Debug.Log("OnConnect(hostId = " + hostId + ", connectionId = " + connectionId + ", error = " + error.ToString() + ")"); }

void OnDisconnect(int hostId, int connectionId, NetworkError error) { Debug.Log("OnDisconnect(hostId = " + hostId + ", connectionId = " + connectionId + ", error = " + error.ToString() + ")"); }

void OnBroadcast(int hostId, byte[] data, int size, NetworkError error) { Debug.Log("OnBroadcast(hostId = " + hostId + ", data = " + data + ", size = " + size + ", error = " + error.ToString() + ")"); }

void OnData(int hostId, int connectionId, int channelId, byte[] data, int size, NetworkError error) { Debug.Log("OnDisconnect(hostId = " + hostId + ", connectionId = " + connectionId + ", channelId = " + channelId + ", data = " + data + ", size = " + size + ", error = " + error.ToString() + ")"); } }