Exemple #1
0
        private void init()
        {
            _redis = new RedisManager2<WeChatRedisConfig>();
            MQManager.RegisterConsumerProcessor<NoticeMQConfig>(async delegate (BasicDeliverEventArgs ar, IModel channel)
            {
                try
                {
                    var rawObject = BinarySerializationHelper.DeserializeObject(ar.Body) as NoticeMQ;


                    var iso = new IsoDateTimeConverter();
                    iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";

                    if(rawObject.Id == 0)
                    {
                        //放入sqlserver 
                        rawObject.Id = await NoticeHelper.SaveNoticeToSql(rawObject);
                        if(rawObject.Id != 0)
                        {
                            //收信人的未读列表加1
                            await NoticeRedisOp.AddUnreadScore(rawObject.Receiver_Uuid, 1);
                            //放入redis
                            try
                            {
                                WeChatNoticeRedis redisObject = new WeChatNoticeRedis();
                                redisObject.Uuid = rawObject.Receiver_Uuid.ToString();
                                redisObject.Message = JsonConvert.SerializeObject(rawObject, iso);
                                //redisObject.Message = BinarySerializationHelper.BytesToHexString(
                                //    BinarySerializationHelper.SerializeObject(rawObject));
                                await _redis.SaveObjectAsync(redisObject);
                            }
                            catch(Exception ex)
                            {
                                LogHelper.LogErrorAsync(typeof(NoticeService), ex);
                            }
                        }
                    }
                    else
                    {
                        //按照id修改sqlserver
                        await NoticeHelper.SaveNoticeToSql(rawObject);
                        //按照id修改redis
                        try
                        {
                            WeChatNoticeRedis redisObject = new WeChatNoticeRedis();
                            redisObject.Uuid = rawObject.Receiver_Uuid.ToString();
                            redisObject.Message = JsonConvert.SerializeObject(rawObject, iso);
                            //redisObject.Message = BinarySerializationHelper.BytesToHexString(
                            //    BinarySerializationHelper.SerializeObject(rawObject));

                            rawObject.status = 0;
                            WeChatNoticeRedis originalRedisObject = new WeChatNoticeRedis();
                            originalRedisObject.Uuid = rawObject.Receiver_Uuid.ToString();
                            originalRedisObject.Message = JsonConvert.SerializeObject(rawObject, iso);
                            //originalRedisObject.Message = BinarySerializationHelper.BytesToHexString(
                            //    BinarySerializationHelper.SerializeObject(rawObject));

                            if(!await _redis.ReplaceObjectInListAsync(redisObject, originalRedisObject))
                                LogHelper.LogInfoAsync(typeof(NoticeService), "替换失败");
                        }
                        catch(Exception ex)
                        {
                            LogHelper.LogErrorAsync(typeof(NoticeService), ex);
                        }
                    }

                }
                catch (Exception ex)
                {
                    LogHelper.LogErrorAsync(typeof(NoticeService), ex);

                    //TODO:错误处理
                }
                finally
                {
                    channel.BasicAck(ar.DeliveryTag, false);
                    System.Threading.Thread.Sleep(1);
                }
            });

            MQManager.Prepare_All_C_MQ();
        }
Exemple #2
0
 /// <summary>
 /// 获取会话中最近的一个消息
 /// </summary>
 /// <param name="fromUuid"></param>
 /// <param name="toUUid"></param>
 /// <returns></returns>
 public static async Task<Message> GetFirstNoticeFromRedis(Guid uuid)
 {
     var db = _redis.GetDb<WeChatNoticeRedis>();
     var config = Configuration.BK_ConfigurationManager.GetConfig<UserBehaviorConfig>();
     int count = Convert.ToInt32(config.GetMessageCount);
     WeChatNoticeRedis o = new WeChatNoticeRedis();
     o.Uuid = uuid.ToString();
     string listName = _redis.GetKeyName<NoticeListAttribute>(o);
     RedisValue[] messages = await db.ListRangeAsync(listName, 0, 0);
     List<string> resultStrings = _redis.ConvertRedisValueToString(messages);
     if (resultStrings != null && !string.IsNullOrEmpty(resultStrings[0]))
     {
         return WeChatNoticeHelper.GetMessageType(
                 JsonConvert.DeserializeObject<NoticeMQ>(resultStrings[0]));
         //return WeChatNoticeHelper.GetMessageType(
         //            BinarySerializationHelper.DeserializeObject(
         //                BinarySerializationHelper.HexStringToBytes(resultStrings[0])) as NoticeMQ);
     }
     else
         return null;
 }
Exemple #3
0
        public async Task<HttpResponseMessage> PostAddContact([FromBody]DualParameter postParameter)
        {
            string openid = postParameter.openID;
            long itemId = postParameter.itemId;

            using(NoticeRepository noticeRepository = new NoticeRepository())
            {
                Message m = await noticeRepository.GetNotice(itemId);

                NoticeMQ rawObject = new NoticeMQ();
                rawObject.Id = m.ID;
                rawObject.MsgType = NoticeType.Contact_Request;
                rawObject.Relation_Uuid = m.RelationID_uuid;
                rawObject.Receiver_Uuid = m.Receiver_uuid;
                rawObject.PayLoad = m.MessageInfo;
                //改了
                rawObject.status = 1;
                rawObject.TimeStamp = m.SendTime;

                var iso = new IsoDateTimeConverter();
                iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";

                RedisManager2<WeChatRedisConfig> _redis = new RedisManager2<WeChatRedisConfig>();
                WeChatNoticeRedis redisObject = new WeChatNoticeRedis();
                redisObject.Uuid = rawObject.Receiver_Uuid.ToString();
                redisObject.Message = JsonConvert.SerializeObject(rawObject, iso);
                //改回
                rawObject.status = 0;
                rawObject.TimeStamp = DateTime.Now;
                WeChatNoticeRedis originalRedisObject = new WeChatNoticeRedis();
                originalRedisObject.Uuid = rawObject.Receiver_Uuid.ToString();
                originalRedisObject.Message = JsonConvert.SerializeObject(rawObject, iso);

                await _redis.ReplaceObjectInListAsync(redisObject, originalRedisObject);
                return WebApiHelper.HttpRMtoJson(null, HttpStatusCode.OK, customStatus.Fail);
            }
        }
Exemple #4
0
 /// <summary>
 /// 从redis中获取谁发的这条消息的集合。按照10步长取。
 /// </summary>
 /// <param name="from"></param>
 /// <param name="to"></param>
 /// <returns>uuid:message的格式</returns>
 static async Task<List<Message>> GetNoticeFromRedis(Guid uuid)
 {
     var db = _redis.GetDb<WeChatNoticeRedis>();
     var config = Configuration.BK_ConfigurationManager.GetConfig<UserBehaviorConfig>();
     int count = Convert.ToInt32(config.GetMessageCount);
     WeChatNoticeRedis o = new WeChatNoticeRedis();
     o.Uuid = uuid.ToString();
     string listName = _redis.GetKeyName<NoticeListAttribute>(o);
     RedisValue[] messages = await db.ListRangeAsync(listName, 0, count - 1);
     await db.ListTrimAsync(listName, 0, count - 1);
     List<string> resultStrings = _redis.ConvertRedisValueToString(messages);
     if (resultStrings == null)
         return null;
     else
     {
         List<Message> result = new List<Message>();
         foreach (var strings in resultStrings)
         {
             result.Add(
                 WeChatNoticeHelper.GetMessageType(
                     JsonConvert.DeserializeObject<NoticeMQ>(strings)));
             //result.Add(
             //    WeChatNoticeHelper.GetMessageType(
             //        BinarySerializationHelper.DeserializeObject(
             //            BinarySerializationHelper.HexStringToBytes(strings)) as NoticeMQ));
         }
         return result;
     }
 }