I'm trying to create a simple hello world with SignalR. I have a hub,
public class MyHub : Hub
{
public void TestConnection(string message)
{
Clients.Caller.testConnection(message);
}
}
and a JS file (using Angular),
var hub = $.connection.myHub;
hub.client.testMessage = function (message) {
console.log("Test: " + message);
};
$.connection.hub.start().done(function () {
alert('signalR started');
}).fail(function (reason) {
console.log("SignalR connection failed: " + reason);
});
hub.server.testConnection("hello signalR").done(function () { });
When I load the page, I just want the console to say "hello signalR". I get a the error message: SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has started.
What am I doing wrong?
I'm trying to create a simple hello world with SignalR. I have a hub,
public class MyHub : Hub
{
public void TestConnection(string message)
{
Clients.Caller.testConnection(message);
}
}
and a JS file (using Angular),
var hub = $.connection.myHub;
hub.client.testMessage = function (message) {
console.log("Test: " + message);
};
$.connection.hub.start().done(function () {
alert('signalR started');
}).fail(function (reason) {
console.log("SignalR connection failed: " + reason);
});
hub.server.testConnection("hello signalR").done(function () { });
When I load the page, I just want the console to say "hello signalR". I get a the error message: SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has started.
What am I doing wrong?
Share Improve this question edited Oct 30, 2015 at 15:50 Aaron Gates asked Oct 30, 2015 at 15:43 Aaron GatesAaron Gates 4694 silver badges15 bronze badges 2-
Did you add the
start().fail()
to see if the initialization was failing for some reason? – Ron Beyer Commented Oct 30, 2015 at 15:46 - @RonBeyer I added it, but it's giving the same error. – Aaron Gates Commented Oct 30, 2015 at 15:51
2 Answers
Reset to default 5Try this:
$.connection.hub.start().done(function () {
alert('signalR started');
hub.server.testConnection("hello signalR").done(function () { });
});
And on the server :
Clients.All.testConnection(message);
Functions from hub.server must be called after the connection has been started. In your example the javascript was being run but the hub connection hasn't finished starting so you put it in the callback to the .done()
This may not solve the issue but you should be calling testMessage from the C# method, not testConnection.