示例#1
0
文件: PushHandler.cs 项目: Daoting/dt
 /// <summary>
 /// 主动停止接收推送
 /// </summary>
 public static void StopRecvPush()
 {
     RetryTimes = 0;
     if (_reader != null && !_reader.IsClosed)
     {
         // 只能通过服务端取消连接!!!
         AtMsg.Unregister(Kit.UserID, _sessionID);
     }
 }
示例#2
0
文件: PushHandler.cs 项目: Daoting/dt
        /// <summary>
        /// 处理服务器推送
        /// </summary>
        /// <returns></returns>
        public static async void Register()
        {
            if ((_reader != null && !_reader.IsClosed) ||
                !Kit.IsLogon)
            {
                //Kit.Msg("已连接");
                return;
            }

#if WASM
            Dict dt = new Dict
            {
                { "sessionid", _sessionID },
                { "model", "wasm" },
                { "name", "Chrome" },
                { "platform", "Browser" },
                { "version", "11.0" },
            };
#else
            Dict dt = new Dict
            {
                { "sessionid", _sessionID },
                { "model", DeviceInfo.Model },
                { "name", DeviceInfo.Name },
                { "platform", DeviceInfo.Platform.ToString() },
                { "version", DeviceInfo.VersionString },
            };
#endif

            try
            {
                _reader = await AtMsg.Register(dt);
            }
            catch
            {
                _reader = null;
                // 小于最大重试次数重连
                if (RetryTimes < _maxRetry)
                {
                    RetryTimes++;
                    //Kit.Msg($"第{RetryTimes}次重连");
                    _ = Task.Delay(TimeSpan.FromSeconds(Math.Pow(2, RetryTimes))).ContinueWith((t) => Register());
                }
                else
                {
                    Kit.Warn($"已重试{_maxRetry + 1}次,无法接收推送!");
                }
                return;
            }

            // 连接成功,重连次数复位
            RetryTimes = 0;
            bool allowRetry = true;
            try
            {
                while (await _reader.MoveNext())
                {
                    var msg = _reader.Val <string>();
                    if (msg == ":Close")
                    {
                        allowRetry = false;
                    }
                    else
                    {
                        new PushHandler().Call(msg);
                    }
                }
            }
            catch { }

            if (allowRetry)
            {
                // 默认允许重连,除非服务端主动取消连接
                _ = Task.Run(() => Register());
            }
            //else
            //{
            //    Kit.Msg("已停止接收推送!");
            //}
        }