等待Local主机在ASP.NET核心中重新加载

发布于 01-22 15:07 字数 681 浏览 2 评论 0原文

我有一个ASP.NET Core Server,它使用SignalR在运行时通过JavaScript动态生成HTML页面。关闭应用程序后,在控制台中,我可以读到SignalR已断开连接:

signalr.js:2404 Error: Connection disconnected with error 'Error: Websocket closed with status code: 1006 ()'.

问题是 - 我需要做些什么来设置Signalr才能在断开连接后再次等待接入连接?这意味着,当我的应用程序再次启动时,预加载的Localhost页面将自动连接并继续使用我的应用程序的新实例。

我在JS中初始化Signalr的方式:

var connection = new signalR.HubConnectionBuilder()
    .withUrl("/foo")
    .build();

然后

connection.on("state", function (state) {
    // some business logics there
});

connection.start().catch(function err() {
    return console.error(err.ToString());
});

I have an ASP.NET Core server, which is using SignalR to generate HTML pages dynamically at runtime through JavaScript. After i shut my application down, in the console i can read that SignalR is disconnected:

signalr.js:2404 Error: Connection disconnected with error 'Error: Websocket closed with status code: 1006 ()'.

The question is - what do i need to do to setup SignalR to wait incoming connection again after disconnecting? Meaning that when my application will be up again, preloaded localhost page will connect automatically and continue working with the new instance of my app.

The way i initialize SignalR in JS:

var connection = new signalR.HubConnectionBuilder()
    .withUrl("/foo")
    .build();

And then

connection.on("state", function (state) {
    // some business logics there
});

connection.start().catch(function err() {
    return console.error(err.ToString());
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2025-01-29 15:07:21

据我所知,SignalR客户端库提供自动重新连接功能。您可以使用witamationReconnect启用它。请注意:没有任何参数,带有AutomationReconnect的参数将分别等待0、2、10和30秒,然后再尝试每个重新连接尝试。四次失败尝试后,它停止尝试重新连接。

您也可以编写代码以手动重新连接。更多详细信息,您可以参考此文章

As far as I know, Signalr Client library provide the auto reconnect feature. You could enable it by using the withAutomaticReconnect. Please notice: Without any parameters, WithAutomaticReconnect configures the client to wait 0, 2, 10, and 30 seconds respectively before trying each reconnect attempt. After four failed attempts, it stops trying to reconnect.

Also you could write codes to do Manually reconnect. More details, you could refer to this article.

落墨 2025-01-29 15:07:21

白兰度的回答使我做了以下事情:

var connection = new signalR.HubConnectionBuilder()
    .withUrl("/foo")
    .build();

function start() {
    connection.start().catch(function () {
        setTimeout(function () {
            start();
            //there we can process any HTML changes like
            //disabling our loader etc. start() will fail
            //with exception if there's no server started
            //and it will try again in 5 seconds
        }, 5000);
    });
}

connection.onclose(e => {
    //process here any HTML changes like
    //showing the loader, etc, an then
    start();
})

connection.on("foo", function (state) {
    // some business logics there
});

start();

看起来像ASP.NET和Signalr + Vanilla JavaScript之间的典型关系

Brando's answer lead me to do the following thing:

var connection = new signalR.HubConnectionBuilder()
    .withUrl("/foo")
    .build();

function start() {
    connection.start().catch(function () {
        setTimeout(function () {
            start();
            //there we can process any HTML changes like
            //disabling our loader etc. start() will fail
            //with exception if there's no server started
            //and it will try again in 5 seconds
        }, 5000);
    });
}

connection.onclose(e => {
    //process here any HTML changes like
    //showing the loader, etc, an then
    start();
})

connection.on("foo", function (state) {
    // some business logics there
});

start();

It looks like typical relationship between ASP.NET and signalR + vanilla javascript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文