Example #1
0
        void Start()
        {
            InitializePacketObservables();

            var bandwidthInterval = TimeSpan.FromMilliseconds(100);

            Observable.Interval(bandwidthInterval).Subscribe(_ =>
            {
                var ts = TimeUtils.NowTimestamp - 1000;
                bandwidth.Flush(ts);
                _receivedBytes.SetValueAndForceNotify(bandwidth.GetReceivedBytesPerSeconds(ts));
                _sentBytes.SetValueAndForceNotify(bandwidth.GetSentBytesPerSecond(ts));
            }).AddTo(this);

            var heartbeatInterval = TimeSpan.FromSeconds(Config.HeartbeatInterval);

            Observable.Interval(heartbeatInterval).SkipUntil(ReadyObservable).Subscribe(_ =>
            {
                SendImmediate(new HeartbeatPacket());
            }).AddTo(this);

            var flushInterval = TimeSpan.FromMilliseconds(300);

            Observable.Interval(flushInterval).SkipUntil(ReadyObservable).Subscribe(_ => Flush()).AddTo(this);

            ErrorRaised.Subscribe(msg =>
            {
                Debug.LogError($"Error: " + ws.error);

                if (loop != null)
                {
                    loop.Dispose();
                    loop = null;
                }

                // dont destory on load로 등록된 네트워크 관련 객체 삭제
                // 네트워크 접속 실패시 처음부터 재시작하는게 목적
                // TOOD 최초 접속 에러와 게임 도중 접속 에러를 분리하기
                Destroy(PingChecker.Instance.gameObject);
                Destroy(ConnectionManager.Instance.gameObject);

                SceneManager.LoadScene(initialScene, LoadSceneMode.Single);
            }).AddTo(this);

            loop = Observable.FromCoroutine(BeginLoop).Subscribe();
        }