OnConnected method not called SignalR when I use s

2019-06-21 06:08发布

问题:

We can create multiple hubs for different things, and to connect to each hub we can create multiple client side hubs with sharing connection so that, one connection being made to all hubs. Now, the problem arises that the hub onconnected method is not raising on each hub server side code.

public class Hub1 : Hub
{
        public override Task OnConnected()
        {
            return base.OnConnected();

        }
}

public class Hub2 : Hub
{
        public override Task OnConnected()
        {
            return base.OnConnected();

        }
}

let say, on the client side i create the hub1 and hub2 with client side methods defined on both the hubs, then only one of the hubs onConnected method gets called on server side. If I create the hubs on the client side with separate connections then the OnConnected method gets called. So, there any other work around if I want to use the same connection for each hub but also would like to raise the on Connected event of separate hubs.

回答1:

I tested it by putting debug point on both hubs, and the OnConnected does get invoked on both hubs, as long as you have any subscriptions to both hubs.

See here: Can I debug OnConnected method in SignalR Hub?

Long story short: By design, if you don't have any subscriptions to the hub, then the javascript client can't get any messages from server so the OnConnected won't get called.

EDIT

See here the Note part:

Note: For JavaScript clients you have to register at least one event handler before calling the Start method to establish the connection.

See more at the link.