0

On my MVC4 ASP.NET web page, I have the code below within the javascript for dealing with the connection to the server and re-connection to the server (if connection is lost with the server for some reason).

The trouble is $.connection.hub.start().done() is not getting called in response to $.connection.hub.start() for the reconnect (its only getting called when it is issued the first time). Is this a bug I wonder? If not, is there an equivalent way to alert the client that start() has completed.

// Start the signalR connection and request the default page
$.connection.hub.start().done(function () {
    console.info("$.connection.hub.start().done");
    hubProxy.server.sendDefaultPage();
});


//if server goes away - reconnect to it
$.connection.hub.disconnected(function () {
    console.info("disconnected");
    setTimeout(function () {
        $.connection.hub.start();
    }, 5000); // Restart connection after 5 seconds.
});
4

1 回答 1

1

您可以将连接启动逻辑放入一个函数中:

function startConnection() {
    // Start the signalR connection and request the default page
    $.connection.hub.start().done(function () {
        console.info("$.connection.hub.start().done");
        hubProxy.server.sendDefaultPage();
    });
}

//if server goes away - reconnect to it
$.connection.hub.disconnected(function () {
    console.info("disconnected");
    setTimeout(startConnection, 5000); // Restart connection after 5 seconds.
});

然后将在初始连接和重新连接时调用 .done() 回调。

于 2013-09-04T18:45:07.497 回答