有没有办法从SignalR中的Clients.method调用中排除客户端?

Etc*_*tch 3 signalr

我正在评估SignalR(恰好与Knockoutjs一起使用),看看我们是否可以使用它来通知客户端并发问题.基本上用户"a"保存记录并且通知用户"b,c,d,e,f,g".我基本上有一个工作的例子,通知所有客户.所以我想我差不多了.

我遇到了这个链接,它引导我走上我现在所处的道路.我一直在看Github上的文档.

基本上我想从Clients.method()通话中排除单个客户端.我没有看到循环客户端并检查ClientId的方法.我能看到的唯一一个可以实现这一目的的可能是看看使用这些组来跟踪它,但这看起来有点麻烦,但我也遇到了问题.

 public class TicketHub : Hub
{
    static int TotalTickets = 10;

    public void GetTicketCount()
    {
        AddToGroup("ticketClients");
        Clients.setTicketCount(TotalTickets);
    }

    public void BuyTicket()
    {
        if (TotalTickets > 0)
            TotalTickets -= 1;

        RemoveFromGroup("ticketClients");

        //  This will call the method ONLY on the calling client
        //  Caller.updateTicketCountWithNotification(TotalTickets);

        // This will call the method on ALL clients in the group
        Clients["ticketClients"].updateTicketCountNotify(TotalTickets);

        AddToGroup("ticketClients");

        Caller.updateTicketCountDontNotify(TotalTickets);
    }
}
Run Code Online (Sandbox Code Playgroud)

sin*_*ici 9

javascript代码:

<script type="text/javascript">
    $(document).ready(function () {
        var test = $.connection.test;
        $("#btnTest").click(function () {
            test.testMethod();
        });
        test.show = function (text, guid) {
            if (guid != test.guid) //notify all clients except the caller
                alert(text);
        };
        $.connection.hub.start(function () { test.start(); });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

课程:

public class Test : Hub
{
    public void Start()
    {
        Caller.guid = Guid.NewGuid();
    }

    public void TestMethod()
    {
        Clients.show("test", Caller.guid);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 只是为了记录,你也可以使用`$ .connection.hub.id`而不是你自己的guid来做一些像这样的小事. (3认同)