c# - 仅将 SignalR 用于用户会话
问题描述
我有一个在处理上传文件的页面上实现 SignalR 的进度条逻辑。它工作正常并产生正确的进展。
但是,它会为所有用户生成进度条,而不仅仅是上传文件的用户。换句话说,一个用户上传了文件,但该文件上传的进度甚至显示在其他用户/会话的屏幕上,这些用户/会话对其端没有执行任何操作
我确实想出了一个解决方法,我发送一个带有 SignalR 进度调用/信号的用户 ID,并将其与存储在 aspx 上的隐藏字段中的用户 ID 进行比较。如果它们不匹配,我不会生成进度条。但是,此修复程序似乎是一种肮脏的解决方法。
是否有更有效的方法来确保 SignalR 仅在一个会话中工作?
以防万一这是我的代码
protected void btnSubmit_Click(object sender, EventArgs e)
{
HttpFileCollection attachments = null;
try
{
lblMessage.Text = string.Empty;
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); hubContext.Clients.All.AddProgress("Upload has been initiated: ", string.Empty, "0", Context.User.Identity.Name, pageName);
if (fileupload1.HasFile)
{
attachments = Request.Files;
if (attachments.Count > totalnumberoffiles)
{
lblMessage.Text += "Please select only " + totalnumberoffiles + " files.";
lblMessage.Visible = true;
}
else
{
double fileProgressPercentagePortion = 100 / attachments.Count;
double fileProgressPercentage = 0;
double fileProgressPercentageSegment = fileProgressPercentagePortion/6;
for (int i = 0; i < attachments.Count; i++)
{
HttpPostedFile attachment = attachments[i];
if (attachment.FileName == string.Empty)
{
continue;
}
hubContext.Clients.All.AddProgress("Currently processing: ", new System.IO.FileInfo(attachment.FileName).Name, "0",
Context.User.Identity.Name, pageName);
if (attachment.ContentLength > 0 && !String.IsNullOrEmpty(attachment.FileName))
{
hubContext.Clients.Client(hubContext.co).AddProgress("Currently processing: ", new System.IO.FileInfo(attachment.FileName).Name,
fileProgressPercentageSegment, Context.User.Identity.Name, pageName);
ProcessFile(attachment, hubContext, fileProgressPercentageSegment,
fileProgressPercentage);
fileProgressPercentage += fileProgressPercentagePortion;
}
}
}
}
}
catch (Exception e3)
{
}
finally
{
}
}
这是我的 JavaScript
$(function () {
// Reference the auto-generated proxy for the hub.
var progress = $.connection.progressHub;
console.log(progress);
var hfUserAccount = document.getElementById("<%=hfUserAccount.ClientID %>");
// Create a function that the hub can call back to display messages.
progress.client.AddProgress = function (fileName, message, percentage, userAccount, pageName) {
if (userAccount === hfUserAccount.value && pageName === "CheckEFile.aspx") {
ProgressBarModal("show", fileName + " " + message);
document.getElementById("divProgress").style.display = "block";
document.getElementById("divUpload").style.display = "block";
document.getElementById("divProgress").style.width = percentage + "%";
document.getElementById("lblPercentage").innerHTML = parseInt(percentage) + "%";
$("#processingStatus").html("Please Wait. Checking files...");
$('#ProgressMessage').width(percentage);
if (percentage === "100%") {
ProgressBarModal();
}
}
};
$.connection.hub.start().done(function () {
var connectionId = $.connection.hub.id;
console.log(connectionId);
});
});
这是我的枢纽
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace IAACCESS.SignalR
{
public class ProgressHub : Hub
{
static ProgressHub()
{
}
}
}
解决方案
如果您只想响应调用服务器端方法的任何人,则可以Clients.Caller
像这样使用该属性:
// Notice 'Clients.Caller' not 'Clients.All'
hubContext.Clients.Caller.AddProgress("Currently processing: ", new System.IO.FileInfo(attachment.FileName).Name, "0", Context.User.Identity.Name, pageName);
推荐阅读
- json - .Net Core 2.1 ModelBinding 在发布 JSON 时不起作用
- microsoft-cognitive - 如何获取 Azure 认知语音服务的凭据
- c - C - 可以添加邻居的图节点(无向)
- firebase - 错误:无法获取 Firebase 项目项目名称。请确保该项目存在并且您的帐户有权访问它
- eclipse - 自动代码完成不适用于 Eclipse
- python - 在更多线程中使用 Python gRPC 客户端存根时可以提高性能吗?
- python - 如何在 Pandas 中替换 Panel 并将其导出到 excel
- delphi - Delphi 最快的 FileSize 大小 > 10gb
- python - 尝试使用 shutil.move 移动文件时出现 WinError 32 和 Errno 13
- angular - 可观察到的“完成”后退订