可能的 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 | 协议版本不兼容。请检查您的库版本。 |
CRCMismatch | Networking.ConnectionConfig 与另一个端点不匹配。 |
DNSFailure | 提供用于连接的地址无效或无法解析。 |
UsageError | 如果使用不合适的参数值调用任何函数,则会发生此错误。 |
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.