在一个 ASP.NET Web 窗体应用程序中,我们使用 Microsoft.AspNet.SignalR 来更新一些长时间运行的任务的进度,它在本地 IIS 服务器上都可以正常工作,但是在负载均衡器(梭子鱼)后面的 IIS 中托管时它不能工作这是集线器代码
public static void SendProgress(string progressMessage, int progressCount, int totalItems,bool isInitializing)
{
//IN ORDER TO INVOKE SIGNALR FUNCTIONALITY DIRECTLY FROM SERVER SIDE WE MUST USE THIS
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
var percentage =0.0;
//CALCULATING PERCENTAGE BASED ON THE PARAMETERS SENT
if (!isInitializing)
percentage = ((double)(progressCount * 100) / (double)totalItems);
if (Convert.ToDouble(percentage) > 100)
percentage = 100;
//PUSHING DATA TO ALL CLIENTS
hubContext.Clients.All.AddProgress(progressMessage, Convert.ToDouble(percentage.ToString("N2")));
}
这是客户端脚本
$(document).ready(function () {
var progress = jqry.connection.progressHub;
console.log("Test!!!!!!");
console.log(progress);
//var intervalID = setInterval(updateProgress, 250);
progress.client.addProgress = function (message, percentage) {
//at this point server side had send message and percentage back to the client
//and then we handle progress bar any way we want it
//Using a function in Helper.js file we show modal and display text and percentage
updateProgress(percentage);
console.log("Signal R PERCENTAGE " + percentage);
};
//Before doing anything with our hub we must start it
jqry.connection.hub.start().done(function () {
//getting the connection ID in case you want to display progress to the specific user
//that started the process in the first place.
console.log("Signal R Done");
});
});
最初,负载均衡器中未启用 Web 套接字,因此 URL wws://www.example.com/signalR/ 出现 404 错误,但随后我们进行了更改以添加
header
Upgrade: websocket
Connection: Upgrade
它没有给出错误并且状态是成功但它没有调用客户端函数 addProgress
我们不知道如何继续调试问题
更新 当我们仍然直接调用 SignalR 端点(通过没有 Baraccuda 的内部 IP)时,它不起作用,当我们调试 Web 应用程序时
/Z1/signalr/negotiate?clientProtocol=1.5&connectionData= 端点返回 200 OK 响应是
{"Url":"/Z1/signalr","ConnectionToken":"sometoken","KeepAliveTimeout":20.0,"DisconnectTimeout":30.0,"ConnectionTimeout":110.0,"TryWebSockets":true,"ProtocolVersion":"1.5 ","TransportConnectTimeout":5.0,"LongPollDelay":0.0}
/Z1/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken= 端点响应返回 101 交换协议
/Z1/signalr/start?transport=webSockets&clientProtocol=1.5&connectionToken= 端点返回 200 OK 响应是 { "Response": "started" }