信号R在停止连接后短暂锁定
本文关键字:锁定 连接 信号 | 更新日期: 2023-09-27 18:00:55
我使用SignalR 1.2.1(因为我们正在执行的项目有.NET 4.0(。
过程基本上是:
- JS客户端启动hub连接,通过调用
BeginPolling
启动: - CCD_ 2引用CCD_ 5中的
ReportProgress
/GetProgress
showProgress
向客户端显示更新的进度消息importDone
是另一个客户端方法,它停止连接,显示"完成!"并重新启用表单字段
我遇到的问题是,如果我试图开始第二次导入,或者甚至点击一个进行简单window.location
重定向的按钮(完成后立即(,我会延迟大约5-10秒。其他用户也报告说,其他机器的延迟时间长达30秒。
我打开了客户端日志记录,还为signalR提供了stateChanged
功能,连接显示它已关闭,不确定是什么原因导致了延迟/锁定?同时出现在IE和Chrome中。
var progressNotifier = $.connection.progressHub;
progressNotifier.client.showProgress = function (message) {
UpdateProgress(message);
};
progressNotifier.client.importDone = function () {
$.connection.hub.stop(); //stop hub connection
//delayed to show "Done!" temporarily
setTimeout(function () {
$('#message').hide();
$('input').prop('disabled', false); //enable input fields
$('#batchName').focus(); //give focus and clear fields
$('#batchName').val('');
$('#batchDescription').val('');
}, 1000);
};
$.connection.hub.start().done(function () {
$.ajax({
url: "Import/ImportXmlFile",
type: 'POST',
dataType: "json",
data: JSON.stringify(@Html.Raw(Json.Encode(Model))),
contentType: 'application/json;charset=utf-8',
});
progressNotifier.server.beginPolling("@Model.BATCHID");
});
function UpdateProgress(msg) {
$("#result").html(msg);
}
ProgressHub.cs:
public class ProgressHub : Hub
{
private readonly RenewalImportProgress _renewalImportProgress;
public ProgressHub() : this(RenewalImportProgress.Instance) { }
public ProgressHub(RenewalImportProgress renewalImportProgress)
{
_renewalImportProgress = renewalImportProgress;
}
public void BeginPolling(string batchId)
{
_renewalImportProgress.GetProgress(batchId);
}
}
RenewalImportProgress.cs:
public class RenewalImportProgress
{
private readonly static Lazy<RenewalImportProgress> _instance = new Lazy<RenewalImportProgress>(() => new RenewalImportProgress(GlobalHost.ConnectionManager.GetHubContext<ProgressHub>().Clients));
private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(1000);
private readonly Timer _timer;
private string _batchId;
private RenewalImportProgress(IHubConnectionContext clients)
{
Clients = clients;
_timer = new Timer(ReportProgress, null, _updateInterval, _updateInterval);
}
public static RenewalImportProgress Instance{ get { return _instance.Value; } }
private IHubConnectionContext Clients { get; set; }
private void ReportProgress(object state)
{
GetProgress(_batchId);
}
public void GetProgress(string batchId)
{
if (!string.IsNullOrEmpty(batchId)) _batchId = batchId;
Stats stats;
var message = "Preparing file...";
using (var _repository = new ImportRepository())
{
stats = _repository.GetImportStats(_batchId);
}
if (stats != null && stats.ProcessCount != 0)
{
var processedCount = stats.ProcessCount + stats.ErrorCount;
if (stats.BatchCount > processedCount)
{
message = "Processing... " + processedCount.ToString() + " of " + stats.BatchCount.ToString() + " completed.";
Clients.All.showProgress(message);
}
else if (stats.BatchCount == processedCount)
{
message = "Processing file... Done!";
Clients.All.showProgress(message);
Clients.All.importDone();
}
}
}
}
在您的代码中,如果您的ReportProgress方法执行时间超过一秒钟,则计时器将同时在另一个线程中再次执行ReportProgress。
不知道这是否会导致问题,但值得尝试更改计时器,以避免在当前执行完成之前再次被调用:
private RenewalImportProgress(IHubConnectionContext clients)
{
Clients = clients;
_timer = new Timer(ReportProgress, null, _updateInterval, Timeout.Infinite);
}
private void ReportProgress(object state)
{
GetProgress(_batchId);
_timer.Change(_updateInterval, Timeout.Infinite);
}
C#中的System.Threading.Timer似乎不起作用。它每3秒运行一次非常快的