Note that there are some explanatory texts on larger screens.

plurals
  1. POSignalR: Hub OnConnected not called if no client methods
    text
    copied!<p>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.</p> <p>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 <code>gameHub.client.joyStickMethod()</code> in the joystick js.</p> <p><em>It was then I discovered something strange</em></p> <p>It turns out that hub method <code>public override Task OnConnected()</code> that I override to deal with the different types of clients (displays and joysticks) is not invoked unless I define a <code>myHub.client.clientSideMethod()</code>. Here's the example I'm dealing with:</p> <pre><code> var gameHub = $.connection.gameHub; gameHub.client.connection(); $("#joystick").joystick({ hub: gameHub }); gameHub.client.activateJoystick = function () { }; $.connection.hub.qs = "type=joystick&amp;gameId=" + "@Model.Id"; $.connection.hub.start(); //this will invoke on connected </code></pre> <p>You see that empty client side method? That's the reason why <code>OnConnected</code> is called on server side. If I remove that line of code, it will not be called.</p> <pre><code> var gameHub = $.connection.gameHub; gameHub.client.connection(); $("#joystick").joystick({ hub: gameHub }); //gameHub.client.activateJoystick = function () { }; $.connection.hub.qs = "type=joystick&amp;gameId=" + "@Model.Id"; $.connection.hub.start(); //this will invoke on connected </code></pre> <p>This will not work, since there is no client side method on the Hub. I can probably add a <code>RegistrateJoystick</code>method on the Hub and call that, but I think that the behavior is unexpected. My question is therefore:</p> <p><strong>Is there a way to manually "connect" to the hub, so that the <code>OnConnect</code> method will be called?</strong> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload