仅在用户会话中使用SignalR

时间:2019-07-16 13:50:18

标签: c# asp.net webforms 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()
        {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

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