示例#1
0
        /// <summary>
        /// 订阅消息
        /// </summary>
        public async Task SubScribeMessage(long partnerUId)
        {
            try
            {
                if (partnerUId > 0)
                {
                    var onLineUser = userBiz.OnLineUser(partnerUId);
                    if (onLineUser != null && onLineUser.IsOnLine)
                    {
                        //当对方正在互动列表页面停留,通知对方刷新页面
                        await Clients.Client(onLineUser.ConnectionId).SendAsync("receive");
                    }

                    var chatListHub = userBiz.ChatListHub(partnerUId);
                    if (chatListHub != null && chatListHub.IsOnLine)
                    {
                        //当对方正在互动列表页面停留,通知对方刷新页面
                        await Clients.Client(chatListHub.ConnectionId).SendAsync("receive");
                    }

                    var userHub = userBiz.OnChatHub(partnerUId);
                    if (userHub != null && userHub.IsOnLine)
                    {
                        //当对方正在互动列表页面停留,通知对方刷新页面
                        await Clients.Client(userHub.ConnectionId).SendAsync("receive");
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorAsync("ChatListHub.SubScribeMessage", ex);
            }
        }
示例#2
0
        public void OnConnected(string connectionId, long uId, long partnerUId, int connetType)
        {
            try
            {
                #region 在线信息
                if (connetType == 0)
                {
                    var onLineUser = userBiz.OnLineUser(uId);
                    if (onLineUser == null)
                    {
                        onLineUser = new OnLineUserHubEntity()
                        {
                            OnLineId     = Guid.NewGuid(),
                            UId          = uId,
                            ConnectionId = connectionId,
                            IsOnLine     = true,
                            CreateTime   = DateTime.Now,
                            UpdateTime   = DateTime.Now
                        };
                        userBiz.InsertOnLineUserAsync(onLineUser);
                    }
                    else
                    {
                        onLineUser.ConnectionId = connectionId;
                        onLineUser.IsOnLine     = true;
                        onLineUser.UpdateTime   = DateTime.Now;
                        userBiz.UpdateOnLineUserAsync(onLineUser);
                    }
                }
                #endregion

                #region 聊天页面在线状态
                else if (connetType == 1)
                {
                    var chatListHub = userBiz.ChatListHub(uId);
                    if (chatListHub == null)
                    {
                        chatListHub = new ChatListHubEntity()
                        {
                            ChatListHubId = Guid.NewGuid(),
                            UId           = uId,
                            ConnectionId  = connectionId,
                            IsOnLine      = true,
                            CreateTime    = DateTime.Now,
                            UpdateTime    = DateTime.Now
                        };
                        userBiz.InsertChatListHubAsync(chatListHub);
                    }
                    else
                    {
                        chatListHub.ConnectionId = connectionId;
                        chatListHub.IsOnLine     = true;
                        chatListHub.UpdateTime   = DateTime.Now;
                        userBiz.UpdateChatListHubAsync(chatListHub);
                    }
                }
                #endregion

                #region 聊天互动功能
                else if (connetType == 2)
                {
                    if (partnerUId > 0)
                    {
                        var userHub = userBiz.OnChatHub(uId);
                        if (userHub == null)
                        {
                            userHub = new OnChatHubEntity()
                            {
                                OnChatHubId  = Guid.NewGuid(),
                                UId          = uId,
                                PartnerUId   = partnerUId,
                                ConnectionId = connectionId,
                                IsOnLine     = true,
                                CreateTime   = DateTime.Now,
                                UpdateTime   = DateTime.Now
                            };
                            userBiz.InsertOnChatHubAsync(userHub);
                        }
                        else
                        {
                            userHub.ConnectionId = connectionId;
                            userHub.IsOnLine     = true;
                            userHub.PartnerUId   = partnerUId;
                            userHub.UpdateTime   = DateTime.Now;
                            userBiz.UpdateOnChatHubAsync(userHub);
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                LogHelper.ErrorAsync("SocketBiz.OnConnected", ex);
            }
        }