首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SignalR中手动向具有多个集线器的组添加连接

问如何在SignalR中手动向具有多个集线器的组添加连接
EN

Stack Overflow用户
提问于 2015-04-20 19:27:18
回答 1查看 922关注 0票数 0

我有两个集线器,叫做NotificationHub和ReminderHub。将NotificationHub视为主要集线器,将ReminderHub视为我希望与NotificationHub分离的可选集线器。客户端将使用以下典型的服务器集线器方法连接到NotificationHub。

代码语言:javascript
复制
public override Task OnConnected()
{
    return base.OnConnected()
}

有相应的客户端连接

代码语言:javascript
复制
$.connection.hub.start().done(function() {
    subscribeToReminderHub();
});

subscribeToReminderHub();包含以下内容

代码语言:javascript
复制
subscribeToReminderHub = function() {
    reminderProxy = $.connection.reminderHub;

    reminderProxy.server.subscribe().done(function() {
        console.log('subscribed to reminder hub...');
    });
}

reminderProxy.server.subscribe()是指ReminderHub上的以下服务器方法

代码语言:javascript
复制
public async Task Subscribe()
{
    var currentUser = Context.User.Identity.Name.ToUpperInvariant();
    await Groups.Add(Context.ConnectionId, currentUser);
}

这一切都如我所期望的那样起作用。我可以命中服务器Subscribe()方法的一个断点,也可以注销。

订阅了提醒中心..。

但是,如果我试图在ReminderHub中建立分组中的用户调用方法,就不会发生任何事情。我在初始连接.done()回调中定义了两个客户端函数。请考虑以下示例

代码语言:javascript
复制
public void Notify() // ... ReminderHub
{
    // ***** notification chain - step 2
    // ***** this never gets called
    var userId = Context.User.Identity.Name.ToUpperInvariant();
    Clients.Caller.notify();
}
代码语言:javascript
复制
// **** $.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,它将返回到分组

代码语言:javascript
复制
GlobalHost.ConnectionManager.GetHubContext<ReminderHub>().Clients.Group(userId).queryNotifications();

整个问题中最有趣的部分

如果我不介绍第二个集线器,并在NotificationHub的OnConnected上建立这个小组,并将所有逻辑重新分解回唯一的中心,整个过程就会像预期的那样工作。不知怎么的,引入第二个中心并试图在OnConnected之外建立一个小组是行不通的。有人经历过这种情况吗?想法?

更有趣!(而且糟透了!)

如果打开浏览器开发工具并显式地将以下内容粘贴到我的控制台中

reminderProxy.server.notify()

我碰到了ReminderHub's Notify()上的断点!我继续通过Clients.Caller.notify();,客户端函数.notify()甚至在我的客户端JS中都没有被调用。我一点也不明白。绕过所有的摸索关注点,现在我已经引入了ReminderHub,甚至连客户端函数都打不上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-21 14:15:24

您应该确保在启动连接之前准备好所有客户端事件处理程序,否则SignalR将无法完成它们(至少在当前版本中是如此)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29756676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档