有没有办法从SignalR中的Clients.Method调用中排除客户端? [英] Is there a way to exclude a Client from a Clients.method call in SignalR?
本文介绍了有没有办法从SignalR中的Clients.Method调用中排除客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在评估SignalR(恰好与Knockoutjs一起使用),看看是否可以使用它来通知客户端并发问题。基本上,用户"a"保存一条记录,并通知用户"b、c、d、e、f、g"。我基本上有一个通知所有客户的工作示例。所以我想我快到了。
我遇到了这个link,它引导我走上了当前的道路。我还一直在查看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);
}
}
推荐答案
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>
类:
public class Test : Hub
{
public void Start()
{
Caller.guid = Guid.NewGuid();
}
public void TestMethod()
{
Clients.show("test", Caller.guid);
}
}
这篇关于有没有办法从SignalR中的Clients.Method调用中排除客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文