只是要提前清楚,这个问题是关于.Net Core SignalR,而不是以前的版本。
新的SignalR与IIS背后的WebSockets存在问题(我无法让它们在Chrome / Win7 / IIS Express上运行)。因此,我使用服务器发送事件(SSE)。 然而,问题是那些超过2分钟后超时,连接状态从2变为3.自动重新连接已被删除(显然它在以前的版本中无论如何都不能正常工作)。
我现在想要实施一个心跳计时器来阻止客户端超时,每30秒打一次就可以完成这项工作。
11月10日更新
我现在已经设法实现服务器端Heartbeat,基本上取自里卡多佩雷斯' https://weblogs.asp.net/ricardoperes/signalr-in-asp-net-core
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
app.UseSignalR(routes =>
{
routes.MapHub<TheHubClass>("signalr");
});
TimerCallback SignalRHeartBeat = async (x) => {
await serviceProvider.GetService<IHubContext<TheHubClass>>().Clients.All.InvokeAsync("Heartbeat", DateTime.Now); };
var timer = new Timer(SignalRHeartBeat).Change(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(30));
对于HubClass,我添加了public async Task HeartBeat(DateTime now) => await Clients.All.InvokeAsync("Heartbeat", now);
显然,计时器,发送的数据(我只是发送一个DateTime)和客户端方法名称都可以不同。
见下面的评论;不应再使用计时器回调。我现在已经实现了IHostedService(或者更确切地说是抽象的BackgroundService)来做到这一点:
public class HeartBeat : BackgroundService
{
private readonly IHubContext<SignalRHub> _hubContext;
public HeartBeat(IHubContext<SignalRHub> hubContext)
{
_hubContext = hubContext;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await _hubContext.Clients.All.SendAsync("Heartbeat", DateTime.Now, stoppingToken);
await Task.Delay(30000, stoppingToken);
}
}
}
在您的启动课程中,在services.AddSignalR();
之后将其连接:
services.AddHostedService<HeartBeat>();
var connection = new signalR.HubConnection("/signalr", { transport: signalR.TransportType.ServerSentEvents });
connection.on("Heartbeat", serverTime => { console.log(serverTime); });
初始问题的剩余部分
剩下的是如何正确地重新连接客户端,例如IO暂停后(浏览器的计算机进入睡眠状态,丢失连接,更改了Wifis或其他)
我已经实现了一个正常工作的客户端Heartbeat,至少在连接断开之前:
public async Task HeartBeatTock() => await Task.CompletedTask;
客户端:
var heartBeatTockTimer; function sendHeartBeatTock(){ connection.invoke(&#34; HeartBeatTock&#34); } connection.start()。then(args =&gt; { heartBeatTockTimer = setInterval(sendHeartBeatTock,10000); });
例如,在浏览器挂起IO之后,invoke方法将抛出异常 - 由于它是异步的,因此无法通过简单的try / catch捕获。 我试图为HeartBeatTock做的事情就像(伪代码):
function sendHeartBeatTock
try connection.invoke("HeartbeatTock)
catch exception
try connection.stop()
catch exception (and ignore it)
finally
connection = new HubConnection().start()
repeat try connection.invoke("HeartbeatTock")
catch exception
log("restart did not work")
clearInterval(heartBeatTockTimer)
informUserToRefreshBrowser()
现在,由于一些原因,这不起作用。由于异步运行,在代码块执行后,invoke抛出异常。它看起来好像暴露了.catch()方法,但我不确定如何正确地实现我的想法。 另一个原因是,启动一个新连接需要我重新实现所有服务器调用,例如&#34; connection.on(&#34; send&#34; ...) - 这看似愚蠢。
非常感谢有关如何正确实施重新连接客户端的任何提示。
答案 0 :(得分:2)
在IIS后面运行SignalR Core时,这是issue。 IIS将在2分钟后关闭空闲连接。长期计划是添加保持活动消息,作为副作用,将阻止IIS关闭连接。要解决此问题,您可以:
答案 1 :(得分:1)
我现在有了一个可行的解决方案(到目前为止在Chrome和FF中测试过)。希望能够激励你想出更好的东西,或者在你自己想出这样的东西的同时拯救你一点,我在这里发布我的解决方案:
Heartbeat-“Tick”消息(服务器定期ping客户端)在上面的问题中描述。 客户端(“Tock”部分)现在有:
tock方法在发送时捕获错误,并尝试启动新连接。由于计时器一直在运行,我正在注册一个新的连接,然后简单地坐下来等待下一次调用。 把客户放在一起:
// keeps the connection object
var connection = null;
// stores the ID from SetInterval
var heartBeatTockTimer = 0;
// how often should I "tock" the server
var heartBeatTockTimerSeconds = 10;
// how often should I retry after connection loss?
var maxRetryAttempt = 5;
// the retry should wait less long then the TockTimer, or calls may overlap
var retryWaitSeconds = heartBeatTockTimerSeconds / 2;
// how many retry attempts did we have?
var currentRetryAttempt = 0;
// helper function to wait a few seconds
$.wait = function(miliseconds) {
var defer = $.Deferred();
setTimeout(function() { defer.resolve(); }, miliseconds);
return defer;
};
// first routine start of the connection
registerSignalRConnection();
function registerSignalRConnection() {
++currentRetryAttempt;
if (currentRetryAttempt > maxRetryAttempt) {
console.log("Clearing registerHeartBeatTockTimer");
clearInterval(heartBeatTockTimer);
heartBeatTockTimer = 0;
throw "Retry attempts exceeded.";
}
if (connection !== null) {
console.log("registerSignalRConnection was not null", connection);
connection.stop().catch(err => console.log(err));
}
console.log("Creating new connection");
connection = new signalR.HubConnection("/signalr", { transport: signalR.TransportType.ServerSentEvents });
connection.on("Heartbeat", serverTime => { console.log(serverTime); });
connection.start().then(() => {
console.log("Connection started, starting timer.");
registerHeartBeatTockTimer();
}).catch(exception => {
console.log("Error connecting", exception, connection);
});
}
function registerHeartBeatTockTimer() {
// make sure we're registered only once
if (heartBeatTockTimer !== 0) return;
console.log("Registering registerHeartBeatTockTimer");
if (connection !== null)
heartBeatTockTimer = setInterval(sendHeartBeatTock, heartBeatTockTimerSeconds * 1000);
else
console.log("Connection didn't allow registry");
}
function sendHeartBeatTock() {
console.log("Standard attempt HeartBeatTock");
connection.invoke("HeartBeatTock").then(() => {
console.log("HeartbeatTock worked.") })
.catch(err => {
console.log("HeartbeatTock Standard Error", err);
$.wait(retryWaitSeconds * 1000).then(function() {
console.log("executing attempt #" + currentRetryAttempt.toString());
registerSignalRConnection();
});
console.log("Current retry attempt: ", currentRetryAttempt);
});
}
答案 2 :(得分:1)
基于ExternalUse的answer ...的客户端版本
import * as signalR from '@aspnet/signalr'
import _ from 'lodash'
var connection = null;
var sendHandlers = [];
var addListener = f => sendHandlers.push(f);
function registerSignalRConnection() {
if (connection !== null) {
connection.stop().catch(err => console.log(err));
}
connection = new signalR.HubConnectionBuilder()
.withUrl('myHub')
.build();
connection.on("Heartbeat", serverTime =>
console.log("Server heartbeat: " + serverTime));
connection.on("Send", data =>
_.each(sendHandlers, value => value(data)));
connection.start()
.catch(exception =>
console.log("Error connecting", exception, connection));
}
registerSignalRConnection();
setInterval(() =>
connection.invoke("HeartBeatTock")
.then(() => console.log("Client heatbeat."))
.catch(err => {
registerSignalRConnection();
}), 10 * 1000);
export { addListener };