如何使用signalr将消息发送到特定客户端 [英] How to send a message to a particular client using signalr

查看:1343
本文介绍了如何使用signalr将消息发送到特定客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从客户端调用SendChatMessage(string who,string message)方法,但是从客户端调用,我必须传递参数。从哪里我将获得连接客户端的名称。在这种情况下,我不明白作为参数传递什么代替谁。



我尝试过:



/ *以下示例显示了如何在存储在内存中的字典中保留连接和用户信息。字典使用HashSet来存储连接ID。* /

I want to invoke the SendChatMessage(string who,string message) method from client side but from client side, i have to pass parameters. From where i would get the names of the connected clients. In this case, i don't understand what to pass as parameter in place of "who".

What I have tried:

/*The following examples show how i retain connection and user information in a dictionary that is stored in memory. The dictionary uses a HashSet to store the connection id.*/

using System.Collections.Generic;
using System.Linq;

namespace BasicChat
{
    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>();
        }

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

                lock (connections)
                {
                    connections.Remove(connectionId);

                    if (connections.Count == 0)
                    {
                        _connections.Remove(key);
                    }
                }
            }
        }
    }
}



下一个例子显示了来自集线器的连接映射类。该类的实例存储在变量名_connections中。


The next example shows the connection mapping class from a hub. The instance of the class is stored in a variable name _connections.

using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

namespace BasicChat
{
    [Authorize]
    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 override Task OnDisconnected(bool stopCalled)
        {
            string name = Context.User.Identity.Name;

            _connections.Remove(name, Context.ConnectionId);

            return base.OnDisconnected(stopCalled);
        }

        public override Task OnReconnected()
        {
            string name = Context.User.Identity.Name;

            if (!_connections.GetConnections(name).Contains(Context.ConnectionId))
            {
                _connections.Add(name, Context.ConnectionId);
            }

            return base.OnReconnected();
        }
    }
}

推荐答案

这是涵盖您要求的官方文档:< a href =https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections>将SignalR用户映射到连接Microsoft Docs [ ^ ]



使用Google搜索找到了许多解决方案:签名服务器到特定客户 [ ^ ]



这是一个: asp.net - SignalR - 使用(IUserIdProvider)向特定用户发送消息*新2.0.0 * - 堆栈溢出 [ ^ ]
This is the official documentation that covers your requirement: Mapping SignalR Users to Connections | Microsoft Docs[^]

There are a number of solutions found using Google Search: signalr server to specific client[^]

Here is one: asp.net - SignalR - Sending a message to a specific user using (IUserIdProvider) *NEW 2.0.0* - Stack Overflow[^]


这篇关于如何使用signalr将消息发送到特定客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆