与SignalR广播消息时忽略客户端 [英] Ignoring a client when broadcasting a message with SignalR

查看:317
本文介绍了与SignalR广播消息时忽略客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想忽略作出广播消息我的web应用程序请求的客户端。我似乎可以做到这一点的唯一方法是通过缓存当前用户的的ConnectionId:

I want to ignore the client that made the request to broadcast the message my web app. The only way I can seem to do this is by caching the connectionId of the current user:

public class BroadcastHub : Hub
{

    public override Task OnConnected()
    {
        System.Runtime.Caching.MemoryCache.Default.Set(HttpContext.Current.User.Identity.Name + "-connectionId", Context.ConnectionId, new CacheItemPolicy() { Priority = CacheItemPriority.Default, SlidingExpiration = TimeSpan.FromHours(1), AbsoluteExpiration = MemoryCache.InfiniteAbsoluteExpiration });

        return base.OnConnected();
    }

    public override Task OnReconnected()
    {
        System.Runtime.Caching.MemoryCache.Default.Set(HttpContext.Current.User.Identity.Name + "-connectionId", Context.ConnectionId, new CacheItemPolicy() { Priority = CacheItemPriority.Default, SlidingExpiration = TimeSpan.FromHours(1), AbsoluteExpiration = MemoryCache.InfiniteAbsoluteExpiration });

        return base.OnReconnected();
    }

}

这可以让我做的控制器的操作方法如下:

This allows me to do the following in the controller action method:

        IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<BroadcastHub>();
        hubContext.Clients.AllExcept((string)MemoryCache.Default.Get(HttpContext.Current.User.Identity.Name + "-connectionId")).sendAddedPasswordDetail(addedPassword);

这个方法似乎工作... ...但我想这可能是做事的方式不对。有没有更好的办法忽略请求的客户端?

This method seems to work... but I'm thinking it might be the wrong way of doing things. Is there a better way to ignore the requesting client?

推荐答案

有是允许这个特定异常的特定属性,

There is a specific property that allows this particular exception,

hubContext.Clients.Others.YourMethodHere

您可以看到它使用的这里

编辑:
按照注释中的讨论,其他使用时,无法使用 GlobalHost.ConnectionManager.GetHubContext&LT; T&GT;

您要么需要继续使用当前的方法或找到一种方法来委托该活动到 BroadcastHub 有机会获得其他

You will either need to continue using your current method or find a way to delegate this activity to the BroadcastHub to have access to Others.

这篇关于与SignalR广播消息时忽略客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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