Signalr 信号器获取连接Id
我第一次尝试使用SignalR向用户反馈长时间运行的进程的进度 我找到了一些.Net核心示例,但最接近的似乎是使用旧版本的SignalR 我正在努力获取“连接ID”。我已经读了很多这样的问题和答案,但似乎仍然无法得到正确的值 这是我从我找到的演示项目中得到的代码:Signalr 信号器获取连接Id,signalr,Signalr,我第一次尝试使用SignalR向用户反馈长时间运行的进程的进度 我找到了一些.Net核心示例,但最接近的似乎是使用旧版本的SignalR 我正在努力获取“连接ID”。我已经读了很多这样的问题和答案,但似乎仍然无法得到正确的值 这是我从我找到的演示项目中得到的代码: // Helper functions added for test purposes function openConnection() { progressConnection = new signalR.HubConne
// Helper functions added for test purposes
function openConnection() {
progressConnection = new signalR.HubConnectionBuilder().withUrl("/progressDemo").build();
debugger;
progressConnection
.start()
.then(() => {
progressConnectionId = progressConnection.connection.connectionId;
$("#connId").html(progressConnectionId);
$("#startButton").removeAttr("disabled");
$("#dropConnectionButton").removeAttr("disabled");
$("#openConnectionButton").attr("disabled", "disabled");
$("#msg").html("Connection established");
console.log("Connection Id: " + progressConnectionId);
})
.catch(() => {
$("#msg").html("Error while establishing connection");
});
}
错误是“connectionId”在以下行中未定义:
progressConnectionId = progressConnection.connection.connectionId;
任何帮助都将不胜感激 好的。。。这很明显,现在我已经解决了:) 我的中心现在看起来像这样:
public override Task OnConnectedAsync()
{
//Count++;
Interlocked.Increment(ref Count);
base.OnConnectedAsync();
Clients.All.SendAsync("updateCount", Count);
Clients.All.SendAsync("connected", Context.ConnectionId);
return Task.CompletedTask;
}
重要的一行是发送回连接Id的那一行
Clients.All.SendAsync("connected", Context.ConnectionId);
然后在客户端,我监听“connected”并设置connectionId变量:
progressConnection.on("connected", (connectionId) => {
progressConnectionId = connectionId;
$("#connId").html(progressConnectionId);
$("#startButton").removeAttr("disabled");
$("#dropConnectionButton").removeAttr("disabled");
$("#openConnectionButton").attr("disabled", "disabled");
$("#msg").html("Connection established");
console.log("Connection Id: " + progressConnectionId);
});
我想下面会更有效,因为它不会将连接Id发送到所有连接的Id
public override Task OnConnectedAsync()
{
//Count++;
Interlocked.Increment(ref Count);
base.OnConnectedAsync();
Clients.All.SendAsync("updateCount", Count);
Clients.Client(Context.ConnectionId).SendAsync("connected", Context.ConnectionId);
return Task.CompletedTask;
}