如何使用SignalR将消息发送到特定客户端

时间:2021-03-27 16:27:44

If I have multiple clients connected to hub, how would I get the details of all the connected clients from client side(.aspx) using JavaScript.
In the SendChatMessage() method, I have to pass "who" parameter from the client side(.aspx) but how would I get to know the connection Id or username of the a particular clients among so many connected clients.

如果我有多个客户端连接到集线器,我如何使用JavaScript从客户端(.aspx)获取所有连接客户端的详细信息。在SendChatMessage()方法中,我必须从客户端(.aspx)传递“who”参数,但是如何在众多连接的客户端中了解特定客户端的连接ID或用户名。

public class Chathub : Hub
{
    private readonly static ConnectionMapping<string> _connections =
        new ConnectionMapping<string>();

    public void SendChatMessage(string who, string message)
    {
        string name = Context.User.Identity.Name;

        foreach (var connectionId in _connections.GetConnections(who))
        {
            Clients.Client(connectionId).addChatMessage(name + ": "message);
        }
    }

    public override Task OnConnected()
    {
        string name = Context.User.Identity.Name;
        _connections.Add(name, Context.ConnectionId);
        return base.OnConnected();
    }


 public class ConnectionMapping<T>
 {
    private readonly Dictionary<T, HashSet<string>> _connections =
        new Dictionary<T, HashSet<string>>();

    public int Count
    {
        get
        {
            return _connections.Count;
        }
    }

    public void Add(T key, string connectionId)
    {
        lock (_connections)
        {
            HashSet<string> connections;
            if (!_connections.TryGetValue(key, out connections))
            {
                connections = new HashSet<string>();
                _connections.Add(key, connections);
            }

            lock (connections)
            {
                connections.Add(connectionId);
            }
        }
    }

    public IEnumerable<string> GetConnections(T key)
    {
        HashSet<string> connections;
        if (_connections.TryGetValue(key, out connections))
        {
            return connections;
        }

        return Enumerable.Empty<string>();
    }

1 个解决方案

#1


0  

While sending message to specific client, you have to call chathub.server.sendMessage() from client

在向特定客户端发送消息时,您必须从客户端调用chathub.server.sendMessage()

public void SendPrivateMessage(string toUserId, string message)
        {

            string fromUserId = Context.ConnectionId;

            var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId) ;
            var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);

            if (toUser != null && fromUser!=null)
            {
                // send to 
                Clients.Client(toUserId).sendMessage(fromUserId, fromUser.UserName, message); 

                // send to caller user as well to update caller chat
                Clients.Caller.sendMessage(toUserId, fromUser.UserName, message); 
            }

        }

Clients.Caller.sendMessage(toUserId, fromUser.UserName, message); note that this is client method, to update the chat.

Clients.Caller.sendMessage(toUserId,fromUser.UserName,message);请注意,这是客户端方法,用于更新聊天。

Then in client side, call chathub.server.sendMessage(id,msg) where you can pass id of that specific user To get id you have to use jQuery, e.g first you have to save id of each client in client side lets say

然后在客户端,调用chathub.server.sendMessage(id,msg),你可以在那里传递那个特定用户的id要获得id,你必须使用jQuery,例如首先你必须在客户端保存每个客户端的id让我们说

<a id='userid' class="username"></a> 

and on click event you can get the id of that user like

并且在点击事件上,您可以获得该用户的ID

    $("a").on('click',function(){

    var touser = $(this).attr('id'))
    }

chathub.server.sendMeassage(touser,msg);

This may not the complete solution, but you have to do like this.

这可能不是完整的解决方案,但你必须这样做。

There's good post here which shows the idea.

这里有好的帖子显示了这个想法。

#1


0  

While sending message to specific client, you have to call chathub.server.sendMessage() from client

在向特定客户端发送消息时,您必须从客户端调用chathub.server.sendMessage()

public void SendPrivateMessage(string toUserId, string message)
        {

            string fromUserId = Context.ConnectionId;

            var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId) ;
            var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);

            if (toUser != null && fromUser!=null)
            {
                // send to 
                Clients.Client(toUserId).sendMessage(fromUserId, fromUser.UserName, message); 

                // send to caller user as well to update caller chat
                Clients.Caller.sendMessage(toUserId, fromUser.UserName, message); 
            }

        }

Clients.Caller.sendMessage(toUserId, fromUser.UserName, message); note that this is client method, to update the chat.

Clients.Caller.sendMessage(toUserId,fromUser.UserName,message);请注意,这是客户端方法,用于更新聊天。

Then in client side, call chathub.server.sendMessage(id,msg) where you can pass id of that specific user To get id you have to use jQuery, e.g first you have to save id of each client in client side lets say

然后在客户端,调用chathub.server.sendMessage(id,msg),你可以在那里传递那个特定用户的id要获得id,你必须使用jQuery,例如首先你必须在客户端保存每个客户端的id让我们说

<a id='userid' class="username"></a> 

and on click event you can get the id of that user like

并且在点击事件上,您可以获得该用户的ID

    $("a").on('click',function(){

    var touser = $(this).attr('id'))
    }

chathub.server.sendMeassage(touser,msg);

This may not the complete solution, but you have to do like this.

这可能不是完整的解决方案,但你必须这样做。

There's good post here which shows the idea.

这里有好的帖子显示了这个想法。