Legacy Documentation: Version 2017.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

NetworkTransport.Receive

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

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

Parameters

hostId Host ID associated with the event.
connectionId The connectionID that received the event.
channelId The channel ID associated with the event.
buffer The buffer that will hold the data received.
bufferSize Size of the buffer supplied.
receivedSize The actual receive size of the data.
error Error (can be cast to NetworkError for more information).

Returns

NetworkEventType Type of event returned.

Description

Called to poll the underlying system for events.

#pragma strict
public class ConnectExample extends MonoBehaviour {
    var connectionId: int;
    var channelId: int;
    var hostId: int;
    function Start() {
        // Init Transport using default values.
        NetworkTransport.Init();
        // Create a connection config and add a Channel.
        var config: ConnectionConfig = new ConnectionConfig();
        channelId = config.AddChannel(QosType.Reliable);
        // Create a topology based on the connection config.
        var topology: HostTopology = 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
        var error: char;
        connectionId = NetworkTransport.Connect(hostId, "10.0.0.42", 54321, 0, error);
    }
    function Update() {
        var outHostId: int;
        var outConnectionId: int;
        var outChannelId: int;
        var receivedSize: int;
        var error: char;
        var evt: NetworkEventType = NetworkTransport.Receive(outHostId, outConnectionId, outChannelId, buffer, buffer.Length, receivedSize, error);
        switch (evt) {
            case NetworkEventType.ConnectEvent: {
                OnConnect(outHostId, outConnectionId, NetworkErrorerror);
                break;
            }
            case NetworkEventType.DisconnectEvent: {
                OnDisconnect(outHostId, outConnectionId, NetworkErrorerror);
                break;
            }
            case NetworkEventType.DataEvent: {
                OnData(outHostId, outConnectionId, outChannelId, buffer, receivedSize, NetworkErrorerror);
                break;
            }
            case NetworkEventType.BroadcastEvent: {
                OnBroadcast(outHostId, buffer, receivedSize, NetworkErrorerror);
                break;
            }
            case NetworkEventType.Nothing:
                break;
            default:
                Debug.LogError("Unknown network message type received: " + evt);

break; } } function OnConnect(hostId: int, connectionId: int, error: NetworkError) { Debug.Log("OnConnect(hostId = " + hostId + ", connectionId = " + connectionId + ", error = " + error.ToString() + ")"); } function OnDisconnect(hostId: int, connectionId: int, error: NetworkError) { Debug.Log("OnDisconnect(hostId = " + hostId + ", connectionId = " + connectionId + ", error = " + error.ToString() + ")"); } function OnBroadcast(hostId: int, data: char[], size: int, error: NetworkError) { Debug.Log("OnBroadcast(hostId = " + hostId + ", data = " + data + ", size = " + size + ", error = " + error.ToString() + ")"); } function OnData(hostId: int, connectionId: int, channelId: int, data: char[], size: int, error: NetworkError) { Debug.Log("OnDisconnect(hostId = " + hostId + ", connectionId = " + connectionId + ", channelId = " + channelId + ", data = " + data + ", size = " + size + ", error = " + error.ToString() + ")"); } }
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; 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() + ")"); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答