Пример #1
0
        private void init()
        {
            _redis = new RedisManager2<WeChatRedisConfig>();
            MQManager.RegisterConsumerProcessor<BK.Model.Configuration.MQ.WeChatMessageMQConfig>(async delegate (BasicDeliverEventArgs ar, IModel channel)
            {
                try
                {
                    var body = ar.Body;
                    var rawObject = BinarySerializationHelper.DeserializeObject(body) as ChatMessageMQ;
                    //log
                    //StringBuilder sb = new StringBuilder();
                    //sb.AppendLine("msg from:" + rawObject.From);
                    //sb.AppendLine("msg to:" + rawObject.To);
                    //sb.AppendLine("msg uuid:" + rawObject.Uuid);
                    //sb.AppendLine("msg sessionid:" + rawObject.SessionId);
                    //sb.AppendLine("msg time:" + CommonHelper.FromUnixTime(Convert.ToDouble(rawObject.TimeStamp)).ToString());
                    //sb.AppendLine("msg message:" + rawObject.PayLoad.ToString());
                    //LogHelper.LogInfoAsync(typeof(Service1), sb.ToString());

                    //记录日志

                    //更新收信方的未读列表数,更新发送方的时间排序
                    //收信人的未读列表加1
                    await MessageRedisOp.AddUnreadScore(rawObject.To, rawObject.SessionId, 1);
                    //LogHelper.LogInfoAsync(typeof(Service1), "数字是:" + d.ToString());

                    //更新双方的session timestamp
                    await MessageRedisOp.SetOrUpdateTimestampToNow(rawObject.From, rawObject.SessionId);

                    await MessageRedisOp.SetOrUpdateTimestampToNow(rawObject.To, rawObject.SessionId);

                    using (MessageRepository r = new MessageRepository())
                    {
                        try
                        {
                            Guid uid = Guid.Parse(rawObject.Uuid);
                            DateTime t = CommonHelper.FromUnixTime(Convert.ToDouble(rawObject.TimeStamp));
                            await r.AddChatLogAsync(uid, rawObject.From, rawObject.To, rawObject.PayLoad.ToString(), rawObject.SessionId, t);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.LogErrorAsync(typeof(Service1), ex);
                        }

                    }

                    //放入redis
                    //uuid:message
                    try
                    {
                        WeChatMessageRedis redisObject = new WeChatMessageRedis();
                        redisObject.SessionId = rawObject.SessionId;
                        redisObject.Message = makeRedisListMessage(rawObject.From, rawObject.TimeStamp, rawObject.PayLoad.ToString());
                        //rawObject.From + ":" + rawObject.PayLoad.ToString();
                        //LogHelper.LogInfoAsync(typeof(Service1), redisObject.Message);
                        await _redis.SaveObjectAsync(redisObject);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogErrorAsync(typeof(Service1), ex);
                    }

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

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

            //EK Comments
            MQManager.RegisterConsumerProcessor<BK.Model.Configuration.MQ.EKCommentMQConfig>(async delegate (BasicDeliverEventArgs ar, IModel channel)
            {
                try
                {
                    var body = ar.Body;
                    var rawObject = BinarySerializationHelper.DeserializeObject(body) as EKCommentMQObject;
                    

                    //TODO Reis update???


                    using (MessageRepository r = new MessageRepository())
                    {
                        try
                        {
                            Guid uid = Guid.Parse(rawObject.uuid);
                            await r.AddEKCommentAsync(uid,Guid.Parse(rawObject.From),rawObject.To,rawObject.Content,rawObject.Timestamp);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.LogErrorAsync(typeof(Service1), ex);
                        }

                    }

                    //放入redis
                    //uuid:message
                    try
                    {
                        EKCommentRedis redisObject = new EKCommentRedis();
                        redisObject.Id = rawObject.To;
                        redisObject.Comment = makeRedisListMessage(rawObject.From, CommonLib.Util.CommonHelper.ToUnixTime(rawObject.Timestamp), rawObject.Content.ToString());
                       
                        await _redis.SaveObjectAsync(redisObject);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogErrorAsync(typeof(Service1), ex);
                    }

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

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

            //Paper Comments
            MQManager.RegisterConsumerProcessor<BK.Model.Configuration.MQ.PaperCommentMQConfig>(async delegate (BasicDeliverEventArgs ar, IModel channel)
            {
                try
                {
                    var body = ar.Body;
                    var rawObject = BinarySerializationHelper.DeserializeObject(body) as PCommentMQObject;

                    //存入数据库
                    using (MessageRepository r = new MessageRepository())
                    {
                        try
                        {
                            Guid uid = Guid.Parse(rawObject.uuid);
                            await r.AddPaperCommentAsync(uid, Guid.Parse(rawObject.From), rawObject.To, rawObject.Content, rawObject.Timestamp);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.LogErrorAsync(typeof(Service1), ex);
                        }

                    }

                    //放入redis
                    //uuid:message
                    try
                    {
                        PaperCommentRedis redisObject = new PaperCommentRedis();
                        redisObject.Id = rawObject.To;
                        redisObject.Comment = makeRedisListMessage(rawObject.From, CommonLib.Util.CommonHelper.ToUnixTime(rawObject.Timestamp), rawObject.Content.ToString());

                        await _redis.SaveObjectAsync(redisObject);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogErrorAsync(typeof(Service1), ex);
                    }

                }
                catch (Exception ex)
                {
                    LogHelper.LogErrorAsync(typeof(Service1), ex);
                }
                finally
                {
                    channel.BasicAck(ar.DeliveryTag, false);
                    System.Threading.Thread.Sleep(1);
                }
            });

            MQManager.Prepare_All_C_MQ();
        }