SignalR in MVC3, timing and startup / connection issues?
I have a very strange problem with MVC3 and signalr .. I have a simple hub,
[HubName("test")]
public class Test: Hub
{
public object GetStuff()
{
return new { dummy = "Test" };
}
}
And some client side code;
var connection = $.connection.test;
connection.start();
connection.getStuff();
This raises an error;
TypeError: object # has no 'start' method
If I do instead
var connection = $.connection("test");
I am getting a different error;
TypeError: object # has no getStuff method jquery-1.6.4.min.js: 4
POST http: // localhost: 63021 / Controller / test / negotiate 405 (method not allowed)
Notice his attempt to negotiate under the controller for any reason?
Is there a specific route I need to register? What other magic am I not aware of?
UPDATE So playing a bit with the console - the first version actually creates an object that has getStuff () that I can call. But signalr fires because I need to call start () first, which doesn't exist! The second one creates an object that has start (), but it doesn't have getStuff () ..
UPDATE 2 Tried using $ .connection.hub.start instead. This seems to work in the console, but not in the onload page. Perhaps the startup has not completed before the call to the hub is made? Is it asynchronous?
source to share
SignalR connection start is not instantaneous. You can call connection.GetStuff();
if the connection is not yet established. If you want this code to run after establishing a connection to the hub, you must use a callback function.
var connection = $.connection.test;
$.connection.hub.start(function(){
// By convention all exposed hub methods start with lowercase
connection.getStuff();
});
Hub Quickstart: https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs
An in-depth look at the SignalR JavaScript client: https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs
source to share