コード例 #1
0
        public void SendMsg(MsgDOT msgDto)
        {
            using (var db = new ChatServiceContext())
            {
                var _user = db.Users.FirstOrDefault(a => a.UserName == msgDto.ToName);
                if (_user != null)
                {
                    var _msg = new ChatMessage();
                    _msg.From         = db.Users.First(a => a.UserName == msgDto.FromName).UserId;
                    _msg.To           = _user.UserId;
                    _msg.Message      = msgDto.Message;
                    _msg.MessageState = (int)MessageState.newMessage;
                    _msg.SendTime     = DateTime.Now;
                    db.ChatMessages.Add(_msg);
                    db.SaveChanges();
                    msgDto.MessageId = _msg.MessageId;
                    msgDto.Time      = _msg.SendTime;

                    syncSelf(msgDto);
                    PushMsg(msgDto);
                }
                else
                {
                    string _errorMsg = string.Format("消息“{0}”发送失败:未找到 {1} 。", msgDto.Message, msgDto.ToName);
                    Clients.Caller.sysMsg(_errorMsg);
                    if (log.IsErrorEnabled)
                    {
                        log.Error(_errorMsg);
                    }
                }
            }
        }
コード例 #2
0
        ///// <summary>
        ///// 组中发言
        ///// </summary>
        ///// <param name="groupName">组名</param>
        ///// <param name="message">信息内容</param>
        //public void SendMsgInGroup(string groupName, string message)
        //{
        //    Clients.Group(groupName, null).pushMsg(message);
        //}

        /// <summary>
        /// 链接服务器成功,记录用户登录状态。
        /// </summary>
        /// <returns></returns>
        public override Task OnConnected()
        {
            List <MsgDOT> historyMsgs = new List <MsgDOT>();
            var           _userName   = Context.QueryString["userName"];

            using (var db = new ChatServiceContext())
            {
                var user = db.Users.FirstOrDefault(u => u.UserName == _userName);
                if (user == null)
                {
                    user          = new User();
                    user.UserName = _userName;
                    db.Users.Add(user);
                    db.SaveChanges();
                }
                var _onlineUser = onlineUsers.FirstOrDefault(a => a.UserName == _userName);
                if (_onlineUser == null)
                {
                    _onlineUser = new OnlineUser()
                    {
                        UserName = _userName, Connections = new List <Connection>()
                    };
                    onlineUsers.Add(_onlineUser);
                }
                _onlineUser.Connections.Add(new Connection()
                {
                    ConnectionId = Context.ConnectionId, Connected = true, UserAgent = ""
                });

                historyMsgs = db.ChatMessages.Where(a => (a.From == user.UserId && a.To == 1) || (a.From == 1 && a.To == user.UserId))
                              .OrderByDescending(a => a.MessageId)
                              .Select(a => new MsgDOT()
                {
                    MessageId = a.MessageId,
                    FromName  = (a.From == 1 ? "tom" : user.UserName),
                    ToName    = (a.From != 1 ? "tom" : user.UserName),
                    Message   = a.Message,
                    IsRead    = a.MessageState == (int)MessageState.readed,
                    Time      = a.SendTime
                })
                              .Take(10)
                              .ToList();
            }
            //发送状态信息
            string _loginMsg = string.Format("{0} 登录成功,当前共有 {1} 处登录。", _userName, onlineUsers.First(a => a.UserName == _userName).Connections.Count);

            Clients.Caller.sysMsg(_loginMsg);
            if (log.IsInfoEnabled)
            {
                log.Info(_loginMsg);
            }
            //发送历史消息
            Clients.Caller.pushHistoryMsg(historyMsgs);
            Clients.Caller.sysMsg("以上是历史消息");

            return(base.OnConnected());
        }