我有两个集线器,叫做NotificationHub和ReminderHub。将NotificationHub视为主要集线器,将ReminderHub视为我希望与NotificationHub分离的可选集线器。客户端将使用以下典型的服务器集线器方法连接到NotificationHub。
public override Task OnConnected()
{
return base.OnConnected()
}有相应的客户端连接
$.connection.hub.start().done(function() {
subscribeToReminderHub();
});subscribeToReminderHub();包含以下内容
subscribeToReminderHub = function() {
reminderProxy = $.connection.reminderHub;
reminderProxy.server.subscribe().done(function() {
console.log('subscribed to reminder hub...');
});
}reminderProxy.server.subscribe()是指ReminderHub上的以下服务器方法
public async Task Subscribe()
{
var currentUser = Context.User.Identity.Name.ToUpperInvariant();
await Groups.Add(Context.ConnectionId, currentUser);
}这一切都如我所期望的那样起作用。我可以命中服务器Subscribe()方法的一个断点,也可以注销。
订阅了提醒中心..。
但是,如果我试图在ReminderHub中建立分组中的用户调用方法,就不会发生任何事情。我在初始连接.done()回调中定义了两个客户端函数。请考虑以下示例
public void Notify() // ... ReminderHub
{
// ***** notification chain - step 2
// ***** this never gets called
var userId = Context.User.Identity.Name.ToUpperInvariant();
Clients.Caller.notify();
}// **** $.connection.hub.start().done(function() { **** callback
subscribeToReminderHub = function() {
reminderProxy = $.connection.reminderHub;
reminderProxy.server.subscribe().done(function() {
console.log('subscribed to reminder hub...');
});
reminderProxy.client.queryNotifications = function () {
// ***** notification chain - step 1
// ***** this never gets called
reminderProxy.server.notify();
}
reminderProxy.client.notify = function () {
// ***** notification chain - step 3
// ***** this never gets called
}
}启动此通知链,我将从集线器调用Notify()外部,如.注意:我正在传递userId,它将返回到分组
GlobalHost.ConnectionManager.GetHubContext<ReminderHub>().Clients.Group(userId).queryNotifications();整个问题中最有趣的部分
如果我不介绍第二个集线器,并在NotificationHub的OnConnected上建立这个小组,并将所有逻辑重新分解回唯一的中心,整个过程就会像预期的那样工作。不知怎么的,引入第二个中心并试图在OnConnected之外建立一个小组是行不通的。有人经历过这种情况吗?想法?
更有趣!(而且糟透了!)
如果打开浏览器开发工具并显式地将以下内容粘贴到我的控制台中
reminderProxy.server.notify()
我碰到了ReminderHub's Notify()上的断点!我继续通过Clients.Caller.notify();,客户端函数.notify()甚至在我的客户端JS中都没有被调用。我一点也不明白。绕过所有的摸索关注点,现在我已经引入了ReminderHub,甚至连客户端函数都打不上。
发布于 2015-04-21 14:15:24
您应该确保在启动连接之前准备好所有客户端事件处理程序,否则SignalR将无法完成它们(至少在当前版本中是如此)。
https://stackoverflow.com/questions/29756676
复制相似问题