Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从客户端发送数据的信号器_C#_Asp.net Mvc_Signalr - Fatal编程技术网

C# 从客户端发送数据的信号器

C# 从客户端发送数据的信号器,c#,asp.net-mvc,signalr,C#,Asp.net Mvc,Signalr,我想使用signar将数据从服务器端发送到客户端。 我这里有一个任务,它迭代了整个作业。此作业从SignalR继承Hub类,并在其中发送迭代的当前进度。但是,它不会返回我从服务器端发送的数据 public async Task loop() { ProgressHub p = new ProgressHub(); for(var i = 0; i < 100; i++){ await p.SendProgress(i, 100); } } pub

我想使用signar将数据从服务器端发送到客户端。 我这里有一个
任务
,它迭代了整个作业。此作业从SignalR继承
Hub
类,并在其中发送迭代的当前进度。但是,它不会返回我从服务器端发送的数据

public async Task loop()
{
    ProgressHub p = new ProgressHub();

    for(var i = 0; i < 100; i++){
        await p.SendProgress(i, 100);
    }
}

public async Task SendProgress(int currentRow, int rowCount)
{
   string message = "Initializing and Preparing...";
   int percent = (currentRow * 100) / rowCount;


    var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
    var percentage = (currentRow * 100) / rowCount;

    hubContext.Clients.All.AddProgress(message, percent + "%");

    await Task.Delay(100);
}

尝试以不同的方式定义和分配函数。例如:

var progressNotifier = $.connection.progressHub;
console.log(progressNotifier);

progressNotifier.client.addProgress = onAddProgress;

function onAddProgress(currentRow, rowCount) {
    console.log(currentRow);
};

$.connection.hub.start();

我还建议您对这类函数使用一个参数,我过去在使用多个参数时遇到了一些问题。您可以传递一条Json消息,稍后将其反序列化。

console.log(currentRow)写入控制台的内容是什么?@mjwills它不返回任何内容。但是
progressNotifier
返回连接的属性。
var progressNotifier = $.connection.progressHub;

console.log(progressNotifier);

progressNotifier.client.addProgress = function (currentRow, rowCount) {

console.log(currentRow);
};

$.connection.hub.start();
var progressNotifier = $.connection.progressHub;
console.log(progressNotifier);

progressNotifier.client.addProgress = onAddProgress;

function onAddProgress(currentRow, rowCount) {
    console.log(currentRow);
};

$.connection.hub.start();