Ejemplo n.º 1
0
 /// <summary>
 /// 发送消息
 /// </summary>
 public JsonResult SendMsg(string channel, string msg)
 {
     _redisMessageManage.SendMsg(channel, msg.JsonDeserialize <MsgEntity>());
     return(new JsonResult("发送消息OK"));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 初始化事件
        /// <para>想法,消息可以合并在一起一起发送</para>
        /// <para>***本设计主要还是属于聊天室的</para>
        /// <para>***聊天室、个人(群聊和个人类似) 这两者应该分开处理,这里简单统一处理</para>
        /// </summary>
        private void InitChannelEventMethod()
        {
            // 添加渠道后
            _channelManage.EventChannelAdded += (manage, subscriber) =>
            {
                _redisMessageManage.OnSubscribe(subscriber.ChannelName, msg =>
                {
                    var model  = msg.JsonDeserialize <MsgEntity>();
                    model.Data = $"{model.Data}—渠道活动数:{subscriber.DicClientSockets.Count}";
                    subscriber.NotifyAllClient(model);
                });
            };
            // 移除渠道后
            _channelManage.EventChannelRemoveed += (manage, subscriber) =>
            {
                _redisMessageManage.OnUnSubscribe(subscriber.ChannelName);
            };

            // 添加渠道用户后
            _channelManage.EventClientAdded += (subscriber, client) =>
            {
                //client.EventMsgSended += (that, msgEntity) => subscriber.NotifyAllClient(msgEntity);    //聊天室单机非Redis模式,接受到消息后直接转发

                client.EventMsgSended += (that, msgEntity) =>
                {
                    if (that.Status != ClientStatusEnum.OnLine || msgEntity.Type == (int)MsgTypeEnum.禁止发送)
                    {
                        SystemNotify(that, "你已经被禁言 或 发言过于频繁");
                        return;
                    }
                    // 判断是个人群聊还是 聊天室
                    if (client.Channel != client.ClientId)
                    {
                        _redisMessageManage.SendMsg(client.Channel, msgEntity);
                    }
                    else
                    {
                        switch (msgEntity.Type)
                        {
                        case (int)MsgTypeEnum.文本:
                            // 用户不在线保存数据, 这里默认保存
                            _redisMessageManage.SendMsg(msgEntity.ToId, msgEntity, true);
                            break;

                        case (int)MsgTypeEnum.请求添加好友:
                            _redisMessageManage.AddUser(new UserEntity {
                                Status = (int)ClientStatusEnum.OnLine, UserId = msgEntity.ToId, UserName = msgEntity.Data
                            });
                            break;
                        }
                    }
                };

                // LoginNotify(subscriber, client);  // 发送登录消息

                if (client.ClientId != client.Channel)
                {
                    // _redisMessageManage.AddChannelSubscribeUser(client.Channel, client.ClientId);   // 添加在线用户
                }
                else
                {
                    GetUserAndMsgList(client);    //获取好友、历史消息
                }

                // Console.WriteLine($"【{subscriber.ChannelName}】上线数量:{subscriber.DicClientSockets.Count}");
            };
            // 移除渠道用户后
            _channelManage.EventClientRemoved += (subscriber, client) =>
            {
                // LoginNotify(subscriber, client); // 发送登出消息

                if (client.ClientId != client.Channel)
                {
                    // _redisMessageManage.DelChannelSubscribeUser(client.Channel, client.ClientId);   // 移除在线用户
                }
                else
                {
                    _redisMessageManage.DelAllMsg(client.ClientId);
                }
            };
        }