I'm having trouble getting SignalR server-side Hub code to invoke JS client methods. The reverse is working fine - so when my client sends a message to the server it is delivered as expected. I've been fairly careful to avoid obvious traps but I guess I'm still overlooking something. Here's my code:
From MessageHub.cs:
public bool SendMessage( ClientMessage message )
{
...
Clients.All.addMessage("my message");
...
}
Javascript:
$.connection.hub.start()
.done(function () {
messageHub = $.connection.message;
// addMessage is never invoked.
messageHub.client.addMessage = function (message) {
alert('message added');
};
/* // I tried this based on some sample code but still not invoked.
messageHub.addMessage = function (message) {
alert('message added');
};
*/
// This works as expected.
messageHub.server.registerUser(userId);
...
});
As mentioned above, I can't find any obvious deficiencies with the setup but here are a few possibly relevant points:
So, given the above, am I missing something obvious? If not, what is the best way to identify the failure point?
P.S. This isn't specifically related to the question, however, for some reason Fiddler is no longer capturing any traffic from any of my browsers, which makes debugging WS or HTTP traffic a bit challenging - I'm guessing that's a Windows 8 thing...
So, the answer was actually quite simple (though since the last release I have upgraded from 1.0.0.0-rc1 to 1.0.0.0 - but I'm fairly certain that didn't change this scenario signficantly).
I found the answer here: https://stackoverflow.com/a/15074002/32935 (note, specifically, my solution was for the first answer provided - though the second helped me identify the cause).
Essentially, for those who don't want to go over to the original answer, I had to setup my client methods before calling the start()
method against the connection as opposed to in the done()
callback as I was doing. Here's an example:
$.connection.message.client.addMessage = function (message) {
alert( 'Now it works!' );
};
$.connection.hub.start()
.done(function () {
console.log( 'Connection established!' );
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With