等待Local主机在ASP.NET核心中重新加载
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,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.
白兰度的回答使我做了以下事情:
看起来像ASP.NET和Signalr + Vanilla JavaScript之间的典型关系
Brando's answer lead me to do the following thing:
It looks like typical relationship between ASP.NET and signalR + vanilla javascript