[英]SignalR sending data from client
我想使用 SignalR 将数据从服务器端发送到客户端。 我在这里有一个Task
来迭代工作。 此作业从 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);
}
在控制器上调用循环方法。
public async Task<JsonResult> CallSignalR()
{
await loop();
return Json("", JsonRequestBehavior.AllowGet);
}
在 JS 上调用 SignalR
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();
我还建议您对这些类型的函数使用单个参数,过去我在使用多个参数时遇到了一些问题。 您可以传递单个 Json 消息并稍后对其进行反序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.