Example #1
0
        public void Start()
        {
            if (SyncNetworkManager.singleton == null)
            {
                Debug.LogWarning("SyncNetworkManager is not in scene");
                return;
            }
            // when a client starts
            SyncNetworkManager.singleton.onStartClient += (client) =>
            {
                // if it is a slave
                if (SyncNet.isFollower)
                {
                    // register a network handler function that caches the last time msg recieved
                    client.RegisterHandler(CustomMsgType.Time, (netMsg) =>
                    {
                        var msg = netMsg.ReadMessage <SyncTimeMessage>();
                        if (_lastMsg == null || msg.time > _lastMsg.time)
                        {
                            _lastMsg = msg;
                        }
                    });

                    // start coroutine that will proces recieved network message
                    StopAllCoroutines();
                    StartCoroutine(UpdateTimeClient());
                }
            };
            // in case the server restarts, when the client next connects the server, make sure the client's last message is reset to null,
            // otherwise in a rare case when the server app restarts, the server's lastMsg.time will be less than the client's lastMsg.time and time chnages on the server will not sync properly on the client.
            SyncNetworkManager.singleton.onClientConnect += (networkConn) =>
            {
                _lastMsg = null;
            };
        }
Example #2
0
        public void Start()
        {
            SyncNetworkManager.singleton._OnStartClient += (client) =>
            {
                if (SyncNet.isSlave)
                {
                    client.RegisterHandler(CustomMsgType.Time, (netMsg) =>
                    {
                        var msg = netMsg.ReadMessage <SyncTimeMessage>();
                        if (_lastMsg == null || msg.time > _lastMsg.time)
                        {
                            _lastMsg = msg;
                        }
                    });

                    StopAllCoroutines();
                    StartCoroutine(UpdateTimeClient());
                }
            };
        }