首页 > 解决方案 > 仅将 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()
        {

        }
    }
}

标签: c#asp.netwebformssignalr

解决方案


如果您只想响应调用服务器端方法的任何人,则可以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);

推荐阅读