简体   繁体   中英

Use signalr to notify all clients except self

I use Signalr 2.2 to send notifications to javascript clients through Hubs. Clients make HTTP requests to my REST web api (c#) and the my server uses the Signlar Hub to push updates to clients.

I know there is a way to manage the clients that the notifications are sent to but at the moment I always use: ~clients.all~. I don't know how can I tell the Id's of each connection, where should I keep those Ids and how to use them?

I've seen in erlier posts that ~clients.Others~ can be used but that seems to be relating to an older api of Signalr

Thanks

这将工作-

Clients.AllExcept(Context.ConnectionId).hello(message);

You can store connection information in the static dictionary in your Hub class. Here is the sample code

        private static readonly ConcurrentDictionary<string, string> _connections = new ConcurrentDictionary<string, string>();

        public void SayHello(string message)
        {
            Clients.Others.hello(message);
        }

        public override System.Threading.Tasks.Task OnConnected()
        {
            _connections.TryAdd(Context.ConnectionId, string.Empty);
            return base.OnConnected();
        }
        public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
        {
            string value;
            _connections.TryRemove(Context.ConnectionId, out value);
            return base.OnDisconnected(stopCalled);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM