Esempio n. 1
0
        /// <summary>
        /// Sent from server->clients whenever clients join or leave.
        /// This updates each client's list of clients in the session.
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="rsMsg"></param>
        public void OnRollbackSessionClientsMsg(NetworkConnection conn, URollbackSessionClientsMsg rsMsg)
        {
            // Don't do this on the server (or host), as they sent the message.
            if (NetworkServer.active)
            {
                return;
            }

            for (int i = 0; i < rsMsg.clients.Length; i++)
            {
                switch (rsMsg.msgType)
                {
                case URollbackSessionClientsMsgType.Add:
                    if (gameManager.rollbackSession.HasClient(rsMsg.clients[i].Identifier))
                    {
                        continue;
                    }
                    gameManager.rollbackSession.AddClient(rsMsg.clients[i].Identifier, rsMsg.clients[i]);
                    break;

                case URollbackSessionClientsMsgType.Remove:
                    gameManager.rollbackSession.RemoveClient(rsMsg.clients[i].Identifier);
                    break;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Whenever a client connects to the server,
        /// we want to add them to the rollback session and tell all clients to do the same.
        /// </summary>
        /// <param name="conn"></param>
        public override void OnServerConnect(NetworkConnection conn)
        {
            base.OnServerConnect(conn);
            Debug.Log($"Client {conn.connectionId} connected to server.");

            GameObject clientManager = GameObject.Instantiate(clientManagerPrefab.gameObject, Vector3.zero, Quaternion.identity);

            clientManager.GetComponent <ClientManager>().connectionID = conn.connectionId;
            NetworkServer.AddPlayerForConnection(conn, clientManager);
            gameManager.rollbackSession.AddClient(conn.connectionId);

            // Tell clients to add this client to the list.
            URollbackSessionClientsMsg clientsMsg = new URollbackSessionClientsMsg(URollbackSessionClientsMsgType.Add,
                                                                                   gameManager.rollbackSession.Clients.Values.ToArray());

            NetworkServer.SendToAll(clientsMsg);
            ServerOnClientJoined?.Invoke();
        }