コード例 #1
0
        //根据某一个在线用户 获取他所关注的股票codeList
        public List <string> GetOnlineCodeList(string online)
        {
            string key      = $"push{online}";
            var    contains = _cache.Contains(key);

            if (!contains)
            {
                LogWrite.WriteLogError(new Exception($"redis db1在线用户{online}不存在push_online-codeList缓存"));
            }

            var codeList = _cache.Get <List <string> >(key);

            return(codeList);
        }
コード例 #2
0
 /// <summary>
 /// 用户下线,更新在线用户,也就是再hub中断开链接调用
 /// </summary>
 /// <param name="onlineUser"></param>
 public void DelOnlineCache(string onlineUser)
 {
     if (_cache.Contains(_redisOnline))
     {
         IList <string> onlineList = _cache.Get <List <string> >(_redisOnline);
         _cache.Remove(_redisOnline);
         onlineList.Remove(_redisOnline);
         _cache.Set(_redisOnline, onlineList, TimeSpan.FromDays(1));
     }
     else
     {
         var exception = new ApplicationException("redis db1中不存在 在线用户缓存");
         LogWrite.WriteLogError(exception);
     }
 }
コード例 #3
0
        /// <summary>
        /// 重连时
        /// </summary>
        /// <returns></returns>
        public override Task OnReconnected()
        {
            var signalRid = GetSignalRid();

            Console.WriteLine($"session_id{signalRid}重连");
            LogWrite.WriteLogInfo($"-------------------------重新链接----------------------");
            LogWrite.WriteLogInfo($"----sessionId:{signalRid}与TrumguSignalR重新链接-------");
            try
            {
                _service.AddOnlineCache(signalRid);
                _service.ChangeOnline();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogWrite.WriteLogError(e);
            }
            return(base.OnReconnected());
        }
コード例 #4
0
        /// <summary>
        /// 断开连接时
        /// </summary>
        /// <param name="stopCalled"></param>
        /// <returns></returns>
        public override Task OnDisconnected(bool stopCalled)
        {
            var signalRid = GetSignalRid();

            Console.WriteLine($"session_id{signalRid}断开连接");
            LogWrite.WriteLogInfo($"-------------------------断开链接----------------------");
            LogWrite.WriteLogInfo($"----sessionId:{signalRid}与TrumguSignalR断开链接-------");
            try
            {
                _service.DelOnlineCache(signalRid);
                _service.ChangeOnline();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogWrite.WriteLogError(e);
            }
            return(base.OnDisconnected(stopCalled));
        }
コード例 #5
0
        public string GetUserIdByRedis(string sessionId)
        {
            LogWrite.WriteLogInfo("--------------从sessionRedis中得到userID--------");
            if (_sessionCache.Contains(sessionId))
            {
                var sessionKey            = sessionId.Contains("WX_") ? "user_info" : "UserInfo";
                t_bf_sys_userObj userInfo = _sessionCache.HashGet <t_bf_sys_userObj>(sessionId, sessionKey);
                if (userInfo == null)
                {
                    LogWrite.WriteLogError(new ApplicationException($"sessionRedis没有成功解析{sessionId}的用户信息"));
                    return(null);
                }
                string userId = userInfo.id.ToString();
                LogWrite.WriteLogInfo($"从sessionRedis获取到了{sessionId}的userId:{userId}");
                return(userId);
            }

            LogWrite.WriteLogError(new ApplicationException($"sessionRedis中没有找到key为:{sessionId}的用户信息缓存"));
            return(null);
        }
コード例 #6
0
        /// <summary>
        /// 构建用来推送的缓存
        /// push+signal-codeList
        /// </summary>
        private void CreateSignalCodeListCache()
        {
            LogWrite.WriteLogInfo("--------------创建推送缓存 push_online-codeList缓存--------");

            var onlineList = GetOnlineCache();

            if (onlineList == null || onlineList.Count <= 0)
            {
                return;
            }

            foreach (var online in onlineList)
            {
                if (!_cache.Contains(online))
                {
                    LogWrite.WriteLogError(new ApplicationException($"构建推送缓存时,缓存中不存在当前session:{online}为key的 online-userId缓存"));
                    return;
                }

                var userId = _cache.Get <string>(online);
                if (!_cache.Contains(userId))
                {
                    LogWrite.WriteLogError(new Exception($"构建推送缓存时,缓存中不存在用户userid{userId}为key的 user-codeList缓存"));
                    return;
                }

                var codeList = _cache.Get <List <string> >(userId);
                //online的key已经存在,所以我要加个前缀
                string key      = $"push{online}";
                var    contains = _cache.Contains(key);
                if (contains)
                {
                    _cache.Remove(key);
                }

                if (codeList != null && codeList.Count > 0)
                {
                    _cache.Set(key, codeList, TimeSpan.FromDays(1));
                }
            }
        }
コード例 #7
0
        public static void Main(string[] args)
        {
            //删除rabbitMQ积压的数据
            WxPopJob.DeleteQueue();
            Console.WriteLine("删除rabbitMQ积压的数据");
            InitService initService = new InitService();

            //移除在线用户
            initService.InitOnline();
            //重启时,更新所有 userid-codeList的缓存
            initService.CreateUserIdCodeListCache();
            Console.WriteLine("更新所有 userid-codeList的缓存---完毕");
            Console.WriteLine("主进程开始..");
            LogWrite.WriteLogInfo("主进程开始");

            var url = Config.GetValue("SignalRUrl");

            LogWrite.WriteLogInfo($"服务地址{url}");
            Console.WriteLine($"服务地址{url}");
            using (WebApp.Start(url))
            {
                try
                {
                    LogWrite.WriteLogInfo("服务端口开启成功");
                    Console.WriteLine("服务端口开启成功");
                    QuartzJob();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    LogWrite.WriteLogError(e);
                }
                finally
                {
                    Console.ReadKey();
                }
            }
        }