Version: 2022.1

NetworkError

enumeration

切换到手册

描述

可能的 Networking.NetworkTransport 错误。

一组可能的 NetworkTransport 错误。如果 NetworkTransport 函数返回错误,您可以使用 NetworkError 来确定发生的错误的类型。 请务必使用 NetworkTransport.Receive 或类似功能来监听这些类型的事件。

//Create a Button (Create>UI>Button) for the host.

//Attach this Button in the Inspector of your GameObject.

//In Play Mode, click the Button to connect. If the connection works, the details are output to the Console window. If there is an error, the error is output to the console.

using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI;

public class Example : MonoBehaviour { int m_ServerSocket;

HostTopology m_HostTopology;

//These are the Buttons that start the client and server, and the Button for sending messages //Assure that you assign these in the Inspector before testing public Button m_ServerButton;

void Start() { //Set up the Connection Configuration which holds channel information ConnectionConfig config = new ConnectionConfig();

//Create a new Host information based on the configuration created, and the maximum connections allowed (20) m_HostTopology = new HostTopology(config, 20); //Initialise the NetworkTransport NetworkTransport.Init();

//Call the ServerButton function when you click the server Button m_ServerButton.onClick.AddListener(ServerButton); }

void Update() { //These are the variables that are replaced by the incoming message int outHostId; int outConnectionId; int outChannelId; byte[] buffer = new byte[1024]; int receivedSize; byte error;

//Set up the Network Transport to receive the incoming message, and decide what type of event NetworkEventType eventType = NetworkTransport.Receive(out outHostId, out outConnectionId, out outChannelId, buffer, buffer.Length, out receivedSize, out error);

switch (eventType) { //Use this case when there is a connection detected case NetworkEventType.ConnectEvent: { //Call the function to deal with the received information OnConnect(outHostId, outConnectionId, (NetworkError)error); break; }

case NetworkEventType.Nothing: break;

default: //Output the error Debug.LogError("Unknown network message type received: " + eventType); break; } }

//This function is called when a connection is detected void OnConnect(int hostID, int connectionID, NetworkError error) { //Output the given information to the console Debug.Log("OnConnect(hostId = " + hostID + ", connectionId = " + connectionID + ", error = " + error.ToString() + ")"); }

void ServerButton() { byte error; //Open the sockets for sending and receiving the messages on port 54321 m_ServerSocket = NetworkTransport.AddHost(m_HostTopology, 54321); //Connect the "server" NetworkTransport.Connect(m_ServerSocket, "127.0.0.1", 54321, 0, out error); //Check for if there is an error if ((NetworkError)error != NetworkError.Ok) { //Output this message in the console with the Network Error Debug.Log("There was this error : " + (NetworkError)error); } //Otherwise if no errors occur, output this message to the console else Debug.Log("Connected : " + (NetworkError)error); } }

变量

Ok操作已成功完成。
WrongHost指定的主机不可用。
WrongConnection指定的 connectionId 不存在。
WrongChannel指定的通道不存在。
NoResources没有足够的资源可用于处理此请求。
BadMessage不是数据消息。
Timeout连接超时。
MessageToLong消息太长,无法写入缓冲区。
WrongOperation操作不受支持。
VersionMismatch协议版本不兼容。请检查您的库版本。
CRCMismatchNetworking.ConnectionConfig 与另一个端点不匹配。
DNSFailure提供用于连接的地址无效或无法解析。
UsageError如果使用不合适的参数值调用任何函数,则会发生此错误。
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961