NetManager.StartAsClient C# (CSharp) Method

StartAsClient() public method

public StartAsClient ( string name, string roomId, Action response ) : void
name string
roomId string
response Action
return void
	public void StartAsClient (string name, string roomId, Action<ResponseType> response) {

		// Register the client
		connection.name = name;
		JSONObject obj = JSONObject.Create ();
		obj.AddField ("name", name);
		obj.AddField ("roomId", roomId);

		// Request to join the room
		Emit<Response.JoinRoom> ("joinRoom", obj, (Response.JoinRoom res) => {

			// If someone else already has this name, don't continue
			if (!res.nameTaken) {

				// Stop searching for rooms to join and listen for if this room is shut down
				Off ("roomListUpdated", OnUpdateRoomList);
				On ("kick", OnRoomDestroyed);

				// Update ConnectionInfo
				Register (res.client._id, res.room);
			}

			response (res.nameTaken ? ResponseType.NameTaken : ResponseType.Success);
		});
	}

Usage Example

    public void JoinGame(string hostName, string roomId, Action <ResponseType> response)
    {
                #if UNITY_EDITOR
        if (Connected)
        {
            Debug.LogWarning(Game.Name + " is attempting to join the '" + hostName + "' game but it is already connected to the '" + Host + "' game. This is not allowed: be sure to disconnect before joining a new room.");
            return;
        }
                #endif

        // Set the host
        Host = hostName;
        net.StartAsClient(Game.Name, roomId, (ResponseType res) => {
            if (res == ResponseType.Success)
            {
                Connected                  = true;
                net.onDisconnected         = OnDisconnect;
                net.onUpdateDroppedClients = OnUpdateDropped;
            }

            response(res);
        });
    }