当我在多个集线器中使用共享连接时,未调用SignalR的OnConnected方法 [英] OnConnected method not called SignalR when I use shared connection in multiple hubs

查看:37
本文介绍了当我在多个集线器中使用共享连接时,未调用SignalR的OnConnected方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以为不同的事物创建多个集线器,并且要连接到每个集线器,我们可以创建具有共享连接的多个客户端集线器,以便对所有集线器建立一个连接.现在,出现了一个问题,即集线器onconnected方法没有在每个集线器服务器端代码上引发.

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();

        }
}

让我们说,在客户端上,我使用在两个集线器上定义的客户端方法创建hub1和hub2,然后在服务器端仅调用一个集线器onConnected方法.如果我在客户端使用单独的连接创建集线器,则会调用OnConnected方法.因此,如果我想为每个集线器使用相同的连接,但又想引发单独的集线器的on Connected事件,则还有其他解决方法.

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.

推荐答案

我通过将调试点放在两个集线器上进行了测试,并且只要有, OnConnected 都会在两个集线器上被调用对这两个集线器的所有订阅.

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.

请参阅此处:我可以在其中调试OnConnected方法吗?SignalR集线器?

长话短说:根据设计,如果您没有对该中心的任何订阅,则javascript客户端无法从服务器获取任何消息,因此不会调用 OnConnected .

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.

编辑

请参见这里注释部分:

注意:对于JavaScript客户端,您必须注册至少一个事件处理程序,然后调用Start方法建立连接.

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

在链接上查看更多.

这篇关于当我在多个集线器中使用共享连接时,未调用SignalR的OnConnected方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆