实现信号器组

时间:2017-05-03 07:15:37

标签: signalr

我正在为我的工作场所创建一个信号器聊天应用程序,供客户和支持人员使用。它一切正常,但我需要防止客户看到对方,只看到在线的支持人员,同样在支持端让他们只看到在线客户,而不是其他支持人员。我搜索了这个并发现实现这种情况的最佳方法是使用组,使得groupA中的用户(支持人员)只能看到groupB(客户端)中的在线用户,而groupB中的用户只能看到groupA中的在线用户。

这是我的中心课程:

`#地区--- ---方法

    public void Connect(string UserName, int UserID)
    {
        var id = Context.ConnectionId;

        if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
        {                
            ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = UserName, UserID = UserID });
        }
        UserDetail CurrentUser = ConnectedUsers.Where(u => u.ConnectionId == id).FirstOrDefault();
        // send to caller           
        Clients.Caller.onConnected(CurrentUser.UserID.ToString(), CurrentUser.UserName, ConnectedUsers, CurrentMessage, CurrentUser.UserID);
        // send to all except caller client           
        Clients.AllExcept(CurrentUser.ConnectionId).onNewUserConnected(CurrentUser.UserID.ToString(), CurrentUser.UserName, CurrentUser.UserID);

    }

    public void SendMessageToAll(string userName, string message)
    {
        // store last 100 messages in cache
        //AddMessageinCache(userName, message);

        // Broad cast message
        //Clients.All.messageReceived(userName, message);
    }

    public void SendPrivateMessage(string toUserId, string message)
    {
        try
        {
            string fromconnectionid = Context.ConnectionId;
            string strfromUserId = (ConnectedUsers.Where(u => u.ConnectionId == Context.ConnectionId).Select(u => u.UserID).FirstOrDefault()).ToString();
            int _fromUserId = 0;
            int.TryParse(strfromUserId, out _fromUserId);
            int _toUserId = 0;
            int.TryParse(toUserId, out _toUserId);
            List<UserDetail> FromUsers = ConnectedUsers.Where(u => u.UserID == _fromUserId).ToList();
            List<UserDetail> ToUsers = ConnectedUsers.Where(x => x.UserID == _toUserId).ToList();

            if (FromUsers.Count != 0 && ToUsers.Count() != 0)
            {
                foreach (var ToUser in ToUsers)
                {
                    // send to                                                                                            //Chat Title
                    Clients.Client(ToUser.ConnectionId).sendPrivateMessage(_fromUserId.ToString(), FromUsers[0].UserName, FromUsers[0].UserName, message);
                }


                foreach (var FromUser in FromUsers)
                {
                    // send to caller user                                                                                //Chat Title
                    Clients.Client(FromUser.ConnectionId).sendPrivateMessage(_toUserId.ToString(), FromUsers[0].UserName, ToUsers[0].UserName, message);
                }
                // send to caller user
                //Clients.Caller.sendPrivateMessage(_toUserId.ToString(), FromUsers[0].UserName, message);
                //ChatDB.Instance.SaveChatHistory(_fromUserId, _toUserId, message);
                MessageDetail _MessageDeail = new MessageDetail { FromUserID = _fromUserId, FromUserName = FromUsers[0].UserName, ToUserID = _toUserId, ToUserName = ToUsers[0].UserName, Message = message };
                AddMessageinCache(_MessageDeail);
            }
        }
        catch { }
    }

    public void RequestLastMessage(int FromUserID, int ToUserID)
    {
        List<MessageDetail> CurrentChatMessages = (from u in CurrentMessage where ((u.FromUserID == FromUserID && u.ToUserID == ToUserID) || (u.FromUserID == ToUserID && u.ToUserID == FromUserID)) select u).ToList();
        //send to caller user
        Clients.Caller.GetLastMessages(ToUserID, CurrentChatMessages);
    }

    public void SendUserTypingRequest(string toUserId)
    {
        string strfromUserId = (ConnectedUsers.Where(u => u.ConnectionId == Context.ConnectionId).Select(u => u.UserID).FirstOrDefault()).ToString();

        int _toUserId = 0;
        int.TryParse(toUserId, out _toUserId);
        List<UserDetail> ToUsers = ConnectedUsers.Where(x => x.UserID == _toUserId).ToList();

        foreach (var ToUser in ToUsers)
        {
            // send to                                                                                            
            Clients.Client(ToUser.ConnectionId).ReceiveTypingRequest(strfromUserId);
        }
    }

    public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
    {
        var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
        if (item != null)
        {
            ConnectedUsers.Remove(item);
            if (ConnectedUsers.Where(u => u.UserID == item.UserID).Count() == 0)
            {
                var id = item.UserID.ToString();
                Clients.All.onUserDisconnected(id, item.UserName);
            }
        }
        return base.OnDisconnected(stopCalled);
    }
    #endregion

    #region---private Messages---
    private void AddMessageinCache(MessageDetail _MessageDetail)
    {
        CurrentMessage.Add(_MessageDetail);
        if (CurrentMessage.Count > 100)
            CurrentMessage.RemoveAt(0);
    }
    #endregion

    public Task JoinGroup(string groupName)
    {
        return Groups.Add(Context.ConnectionId, groupName);
    }
    public Task LeaveGroup(string groupName)
    {
        return Groups.Remove(this.Context.ConnectionId, groupName);
    }
}

}` 我的Javascript:

$.connection.hub.start().done(function () {
    registerEvents(chatHub);
    chatHub.server.joinGroup("GroupA")

});

0 个答案:

没有答案