【发布时间】:2018-12-13 12:52:47
【问题描述】:
我的 MVC 解决方案中编写了一个 SignalR 集线器,其中一个 Javascript 客户端从视图连接。
连接的目的是从服务器接收对墙板的更改。这必须几乎立即发生并且需要终生连接,因为网页在屏幕上运行,没有直接的 PC 访问。
到目前为止,SignalR 连接工作了几个小时才出现错误。
我得到的错误是
Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message form the server.'.
Failed to load resource: net::ERR_CONNECTION_TIMED_OUT
Warning: Error from HTTP request. 0:
Error: Failed to complete negotiation with the server: Error
Error: Failed to start the connection: Error
Uncaught (in promise) Error
at new HttpError (singlar.js:1436)
at XMLHttpRequest.xhr.onerror (singalr.js:1583)
我的客户代码
let connection = new signalR.HubConnectionBuilder()
.withUrl("/wbHub")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.start().then(function () {
connection.invoke("GetAllWallboards").then(function (wallboard) {
for (var i = 0; i < wallboard.length; i++) {
displayWallboard(wallboard[i]);
}
startStreaming();
})
})
connection.onclose(function () {
connection.start().then(function () {
startStreaming();
})
})
function startStreaming() {
connection.stream("StreamWallboards").subscribe({
close: false,
next: displayWallboard
});
}
中心代码:
public class WallboardHub : Hub
{
private readonly WallboardTicker _WallboardTicker;
public WallboardHub(WallboardTicker wallboardTicker)
{
_WallboardTicker = wallboardTicker;
}
public IEnumerable<Wallboard> GetAllWallboards()
{
return _WallboardTicker.GetAllWallboards();
}
public ChannelReader<Wallboard> StreamWallboards()
{
return _WallboardTicker.StreamWallboards().AsChannelReader(10);
}
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "SignalR Users");
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users");
await base.OnDisconnectedAsync(exception);
}
}
问题 1:我处理重新连接的方式是否正确?从错误中感觉.onclose 有效,但它只尝试一次?无论如何在显示错误之前尝试 x 分钟?
问题 2:重新加载网站使连接再次工作,是否有可能在 signalR 连接错误时刷新浏览器?
【问题讨论】:
标签: .net-core asp.net-core-mvc signalr