简体   繁体   中英

How Can I Manually Add Connections to Groups with Multiple Hubs in SignalR

I have two hubs, call them NotificationHub and ReminderHub . Think of NotificationHub as the main hub, and ReminderHub as an optional hub that I wish to separate from NotificationHub . Clients will connect to NotificationHub with the following typical server hub method.

public override Task OnConnected()
{
    return base.OnConnected()
}

with corresponding client connection

$.connection.hub.start().done(function() {
    subscribeToReminderHub();
});

subscribeToReminderHub(); contains the following

subscribeToReminderHub = function() {
    reminderProxy = $.connection.reminderHub;

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

reminderProxy.server.subscribe() refers to the following server method on ReminderHub

public async Task Subscribe()
{
    var currentUser = Context.User.Identity.Name.ToUpperInvariant();
    await Groups.Add(Context.ConnectionId, currentUser);
}

This all works as I would tyically expect. I can hit a break point on the server Subscribe() method, as well as log out

subscribed to reminder hub...

However, if I try to then invoke methods on users in the groupings I am trying to establish in ReminderHub , nothing will occur. I have defined two client functions inside my initial connection .done() callback. Consider the following example

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
    }
}

Starting this notification chain, I am invoking Notify() external from the hub like... note: I am passing userId which would relate back to the grouping

GlobalHost.ConnectionManager.GetHubContext<ReminderHub>().Clients.Group(userId).queryNotifications();

The most interesting part of this whole issue

is that if I don't introduce a second hub, and establish the group on NotificationHub 's OnConnected and re-factor all logic back to the sole hub, this entire process works as expected. Somehow introducing a second hub and trying to establish a group outside of OnConnected is not working. Has anyone experienced this? Thoughts?

Even more interesting! (and awful!)

if I open up my browser dev tools and explicitly paste the following into my console

reminderProxy.server.notify()

I hit the breakpoint on ReminderHub 's Notify() ! I continue through Clients.Caller.notify(); and the client function .notify() does not even get called in my client JS. I can't understand that at all. Bypassing all groping concerns, I can't even hit a client function now that I've introduced ReminderHub

您应该确保在开始连接之前准备好所有客户端事件处理程序,否则SignalR将无法实现它们(至少在当前版本中)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM