【问题标题】:How to send message to a particular client using SignalR如何使用 SignalR 向特定客户端发送消息
【发布时间】:2017-04-04 11:02:24
【问题描述】:

如果我有多个客户端连接到集线器,我将如何使用 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>();
    }

【问题讨论】:

  • 您不应该在这个问题中添加 C# 标签吗?它似乎与 JavaScript 更相关。

标签: javascript c# asp.net signalr


【解决方案1】:

在向特定客户端发送消息时,您必须从客户端调用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); 注意这是客户端方法,用于更新聊天。

然后在客户端,调用chathub.server.sendMessage(id,msg) 你可以在哪里通过id specific user 要获取 id,你必须使用 jQuery,例如,首先你必须在客户端保存每个客户端的 id 让我们说

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

在点击事件中,您可以获得该用户的id 喜欢

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

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

chathub.server.sendMeassage(touser,msg);

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

here 有很好的帖子说明了这个想法。

【讨论】:

  • 在 SendChatMessage() 方法中,从客户端传递什么来代替“who”作为参数。我认为“谁”是我的字典(包含已连接客户端详细信息的字典)的键,用于查找具有该键的 ConnectionId..
  • 检查编辑,您必须这样做才能实现您的要求。
猜你喜欢
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
相关资源
最近更新 更多