Version: 5.3 (switch to 5.4b)
IdiomaEnglish
  • C#
  • JS

Idioma de script

Selecciona tu lenguaje de programación favorito. Todos los fragmentos de código serán mostrados en este lenguaje.

NetworkClient.Connect

Sugiere un cambio

¡Éxito!

Gracias por ayudarnos a mejorar la calidad de la documentación de Unity. A pesar de que no podemos aceptar todas las sugerencias, leemos cada cambio propuesto por nuestros usuarios y actualizaremos los que sean aplicables.

Cerrar

No se puedo enviar

Por alguna razón su cambio sugerido no pudo ser enviado. Por favor <a>intente nuevamente</a> en unos minutos. Gracias por tomarse un tiempo para ayudarnos a mejorar la calidad de la documentación de Unity.

Cerrar

Cancelar

Cambiar al Manual
public function Connect(serverIp: string, serverPort: int): void;
public void Connect(string serverIp, int serverPort);

Parámetros

serverIp Target IP address or hostname.
serverPort Target port number.

Descripción

Connect client to a NetworkServer instance.

Connecting to a server is asynchronous. There is connection message that is fired when the client connects. If the connection fails, a MsgType.SYSTEM_ERROR message will be generated. Once a connection is established you are able to send messages on the connection using NetworkClient.Send(). If using other features of the high level api, the client should call NetworkClient.IsReady() once it is ready to participate in the game. At that point the client will be sent spawned objects and state update messages.

#pragma strict
public class NetClient {
	var myClient: NetworkClient;
	public function OnConnected(conn: NetworkConnection, reader: NetworkReader) {
		Debug.Log("Connected to server");
		myClient.Ready();
	}
	public function OnDisconnected(conn: NetworkConnection, reader: NetworkReader) {
		Debug.Log("Disconnected from server");
	}
	public function OnError(conn: NetworkConnection, reader: NetworkReader) {
		var errorMsg: SystemErrorMessage = reader.SmartRead.<SystemErrorMessage>();
		Debug.Log("Error connecting with code " + errorMsg.errorCode);
	}
	public function Start() {
		myClient = NetworkClient.Instance;
		myClient.RegisterHandler(MsgType.SYSTEM_CONNECT, OnConnected);
		myClient.RegisterHandler(MsgType.SYSTEM_DISCONNECT, OnDisconnected);
		myClient.RegisterHandler(MsgType.SYSTEM_ERROR, OnError);
		myClient.Connect("127.0.0.1", 8888);
	}
}
using UnityEngine;
using UnityEngine.Networking;

public class NetClient { NetworkClient myClient;

public void OnConnected(NetworkConnection conn, NetworkReader reader) { Debug.Log("Connected to server"); myClient.Ready(); }

public void OnDisconnected(NetworkConnection conn, NetworkReader reader) { Debug.Log("Disconnected from server"); }

public void OnError(NetworkConnection conn, NetworkReader reader) { SystemErrorMessage errorMsg = reader.SmartRead<SystemErrorMessage>(); Debug.Log("Error connecting with code " + errorMsg.errorCode); }

public void Start() { myClient = NetworkClient.Instance;

myClient.RegisterHandler(MsgType.SYSTEM_CONNECT, OnConnected); myClient.RegisterHandler(MsgType.SYSTEM_DISCONNECT, OnDisconnected); myClient.RegisterHandler(MsgType.SYSTEM_ERROR, OnError);

myClient.Connect("127.0.0.1", 8888); } }