SignalR:如果没有客户端方法,则不调用Hub OnConnected [英] SignalR: Hub OnConnected not called if no client methods

查看:97
本文介绍了SignalR:如果没有客户端方法,则不调用Hub OnConnected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SignalR写一个游戏。这个想法是你用你的笔记本电脑连接,它将用作显示器,然后你连接智能手机,它将被用作操纵杆。整个设置运行良好。

I am writing a game in SignalR. The idea is that you connect with your laptop, which will be used as "display" and then you connect with your smart phone and that will be used as "joystick". The entire setup works very well.

现在一切正常,我决定重构代码。我意识到操纵杆和显示器之间的关系是一种方式(显示器永远不需要将信息发送回操纵杆)。因此我决定在操纵杆上不需要任何signalR可调用的客户端函数(例如,操纵杆js中没有 gameHub.client.joyStickMethod()

Now that it all works, I decided to refactor the code. I realized that the relationship between the joystick and the display is one way (the display never needs to send information back to the joystick). Therefore I decided that I did not need any signalR invokable client functions on the joystick (e.g. there is no use for gameHub.client.joyStickMethod() in the joystick js.

然后我发现了一些奇怪的东西

事实证明,hub方法公共覆盖除非我定义 myHub.client,否则不会调用我覆盖以处理不同类型的客户端(显示和操纵杆)的任务OnConnected()。 clientSideMethod()。这是我正在处理的例子:

It turns out that hub method public override Task OnConnected() that I override to deal with the different types of clients (displays and joysticks) is not invoked unless I define a myHub.client.clientSideMethod(). Here's the example I'm dealing with:

        var gameHub = $.connection.gameHub;
        gameHub.client.connection();
        $("#joystick").joystick({
            hub: gameHub
        });

        gameHub.client.activateJoystick = function () { };
        $.connection.hub.qs = "type=joystick&gameId=" + "@Model.Id";
        $.connection.hub.start();   //this will invoke on connected

你看到那个空的客户端方法?这就是为什么在服务器端调用 OnConnected 的原因。如果删除该行代码,则不会调用它。

You see that empty client side method? That's the reason why OnConnected is called on server side. If I remove that line of code, it will not be called.

        var gameHub = $.connection.gameHub;
        gameHub.client.connection();
        $("#joystick").joystick({
            hub: gameHub 
        });

        //gameHub.client.activateJoystick = function () { };
        $.connection.hub.qs = "type=joystick&gameId=" + "@Model.Id";
        $.connection.hub.start();   //this will invoke on connected

这不起作用,因为没有客户端方法毂。我可以在Hub上添加 RegistrateJoystick 方法并调用它,但我认为这种行为是意外的。我的问题是:

This will not work, since there is no client side method on the Hub. I can probably add a RegistrateJoystickmethod on the Hub and call that, but I think that the behavior is unexpected. My question is therefore:

有没有办法手动连接到集线器,以便 OnConnect 方法会被调用吗?

Is there a way to manually "connect" to the hub, so that the OnConnect method will be called?

推荐答案

这是设计的。如果您没有对集线器的任何订阅,那么该特定客户端将无法从服务器到客户端获取任何消息。您仍然可以在集线器上调用方法。

That's by design. If you don't have any subscriptions to the hub then that particular client can't won't get any messages from server to client. You can still invoke methods on the hub.

这篇关于SignalR:如果没有客户端方法,则不调用Hub OnConnected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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