最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SignalR must call .done or .fail before logic - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 5

Try 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.

发布评论

评论列表(0)

  1. 暂无评论