/// <summary>
        /// This logic plugs into the "ConnectionApprovalCallback" exposed by MLAPI.NetworkManager, and is run every time a client connects to us.
        /// See GNH_Client.StartClient for the complementary logic that runs when the client starts its connection.
        /// </summary>
        /// <remarks>
        /// Since our game doesn't have to interact with some third party authentication service to validate the identity of the new connection, our ApprovalCheck
        /// method is simple, and runs synchronously, invoking "callback" to signal approval at the end of the method. MLAPI currently doesn't support the ability
        /// to send back more than a "true/false", which means we have to work a little harder to provide a useful error return to the client. To do that, we invoke a
        /// client RPC in the same channel that MLAPI uses for its connection callback. Since that channel ("MLAPI_INTERNAL") is both reliable and sequenced, we can be
        /// confident that our login result message will execute before any disconnect message.
        /// </remarks>
        /// <param name="connectionData">binary data passed into StartClient. In our case this is the client's GUID, which is a unique identifier for their install of the game that persists across app restarts. </param>
        /// <param name="clientId">This is the clientId that MLAPI assigned us on login. It does not persist across multiple logins from the same client. </param>
        /// <param name="callback">The delegate we must invoke to signal that the connection was approved or not. </param>
        private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
        {
            if (connectionData.Length > k_MaxConnectPayload)
            {
                callback(false, 0, false, null, null);
                return;
            }

            string payload           = System.Text.Encoding.UTF8.GetString(connectionData);
            var    connectionPayload = JsonUtility.FromJson <ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html
            int    clientScene       = connectionPayload.clientScene;

            m_ClientSceneMap[clientId] = clientScene;

            //TODO: GOMPS-78. We'll need to save our client guid so that we can handle reconnect.
            Debug.Log("host ApprovalCheck: client guid was: " + connectionPayload.clientGUID);

            //Populate our dictionaries with the playerData
            m_ClientIDToGuid[clientId] = connectionPayload.clientGUID;
            m_ClientData[connectionPayload.clientGUID] = new PlayerData(connectionPayload.playerName, clientId);

            //TODO: GOMPS-79 handle different error cases.

            callback(false, 0, true, null, null);

            //FIXME_DMW: it is weird to do this after the callback, but the custom message won't be delivered if we call it beforehand.
            //This creates an "honor system" scenario where it is up to the client to politely leave on failure. Probably
            //we should add a NetManager.DisconnectClient call directly below this line, when we are rejecting the connection.
            m_Portal.S2CConnectResult(clientId, ConnectStatus.Success);
        }
        private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
        {
            string payload           = Encoding.ASCII.GetString(connectionData);
            var    connectionPayload = JsonUtility.FromJson <ConnectionPayload>(payload);

            bool approveConnection = connectionPayload.password == passwordInputField.text;

            Vector3    spawnPos = Vector3.zero;
            Quaternion spawnRot = Quaternion.identity;

            if (approveConnection)
            {
                switch (NetworkManager.Singleton.ConnectedClients.Count)
                {
                case 1:
                    spawnPos = new Vector3(0f, 0f, 0f);
                    spawnRot = Quaternion.Euler(0f, 180f, 0f);
                    break;

                case 2:
                    spawnPos = new Vector3(2f, 0f, 0f);
                    spawnRot = Quaternion.Euler(0f, 225, 0f);
                    break;
                }

                clientData[clientId] = new PlayerData(connectionPayload.playerName);
            }

            callback(true, null, approveConnection, spawnPos, spawnRot);
        }
    private IEnumerator RunApprovalCheck(byte[] connectionData,
                                         ulong clientId,
                                         MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
    {
        yield return(new WaitForSeconds(approvalDelay));

        callback(true, null, true, null, null);
    }
Exemple #4
0
    private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
    {
        bool approve            = true;
        bool createPlayerObject = true;

        ulong?prefabHash = NetworkSpawnManager.GetPrefabHashFromGenerator("MyPrefab");

        callback(createPlayerObject, prefabHash, approve, Vector3.zero, Quaternion.identity);
    }
Exemple #5
0
    void OnClientConnectApproval(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
    {
        bool approve            = true;
        bool createPlayerObject = true;

        if (clients.Count == maxPlayer - 1)
        {
            approve = false;
        }
        callback(createPlayerObject, null, approve, null, null);
    }
    private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
    {
        print("approving...");
        // bool approve = true;
        // bool createPlayerObject = false;

        var payload = System.Text.Encoding.UTF8.GetString(connectionData);

        print($"connected {payload} \nClient: {clientId}");

        ulong?prefabHash = NetworkSpawnManager.GetPrefabHashFromGenerator("MyPrefab");
        //callback(createPlayerObject, prefabHash, approve, Vector3.zero, Quaternion.identity);
    }
Exemple #7
0
    private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
    {
        Debug.Log("Client " + clientId + " is joining server with password: "******"MyPrefabHashGenerator" with the name of a prefab added to the NetworkPrefabs field of your NetworkManager object in the scene
        ulong?prefabHash = NetworkSpawnManager.GetPrefabHashFromGenerator("Player_Client");

        ////If approve is true, the connection gets added. If it's false. The client gets disconnected
        callback(createPlayerObject, prefabHash, approve, new Vector3(0, 1, 0), new Quaternion());
    }
Exemple #8
0
    private void HostApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
    {
        print("approving...");

        var payload = System.Text.Encoding.UTF8.GetString(connectionData);

        print($"connected {payload} \nClient: {clientId}");
        if (connectedClientsDict.ContainsKey(clientId))
        {
            print(clientId + " is already in");
            return;
        }

        connectedClientsDict.Add(clientId, payload);

        callback(false, 0, true, Vector3.zero, Quaternion.identity);
        UpdateClients();
        SendConnectionUpdate();
    }
    private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
    {
        //Your logic here
        bool approve            = true;
        bool createPlayerObject = true;

        // The prefab hash. Use null to use the default player prefab
        // If using this hash, replace "MyPrefabHashGenerator" with the name wof a prefab added to the NetworkPrefabs field of your NetworkManager object in the scene
        ulong?prefabHashKid   = NetworkSpawnManager.GetPrefabHashFromGenerator("KID");
        ulong?prefabHashSnail = NetworkSpawnManager.GetPrefabHashFromGenerator("SNAIL");

        //If approve is true, the connection gets added. If it's false. The client gets disconnected
        if (counter % 2 == 1)
        {
            callback(createPlayerObject, prefabHashSnail, approve, snailSpawn[Random.Range(0, snailSpawn.Length)].position, quaternion.identity);
        }
        else
        {
            callback(createPlayerObject, prefabHashKid, approve, kidSpawn[Random.Range(0, kidSpawn.Length)].position, quaternion.identity);
        }

        counter++;
    }
        private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
        {
            string password = Encoding.ASCII.GetString(connectionData);

            bool approveConnection = password == passwordInputField.text;

            Vector3    spawnPos = Vector3.zero;
            Quaternion spawnRot = Quaternion.identity;

            switch (NetworkManager.Singleton.ConnectedClients.Count)
            {
            case 1:
                spawnPos = new Vector3(0f, 0f, 0f);
                spawnRot = Quaternion.Euler(0f, 180f, 0f);
                break;

            case 2:
                spawnPos = new Vector3(2f, 0f, 0f);
                spawnRot = Quaternion.Euler(0f, 225, 0f);
                break;
            }

            callback(true, null, approveConnection, spawnPos, spawnRot);
        }
 private void ApprovalCheck(byte[] connectionData,
                            ulong clientId,
                            MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
 {
     StartCoroutine(RunApprovalCheck(connectionData, clientId, callback));
 }