SignalR Hub 向特定客户端发送信号

SignalR Hub Sending signal to Specific client

我正在尝试向当前与更新程序处于同一 URL 中的其他用户发送警告消息。

假设我在第 .../Customers/Detail/597 页并更新它。

页面重新加载后,我想向当前在同一页面上的用户发送信号 (.../Customers/Detail/597)

目前,即使其他用户像 .../Customers/Detail/687 一样在不同的页面上,他们仍然会收到警报消息。

您可以创建一个群组,以页面的 ID 作为名称。然后,您可以仅向与页面 ID 关联的组发送消息。

https://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#groupsfromhub

这可以使用类似

的方法来完成

在您的 asp.net mvc 端(从您发送消息的位置)

IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<Hub>();
hubContext.Clients.All.NotifyUser(id, message);

并在您的javascript(您接受的地方)

//you must have that Id (from URL) in your javascript too so you can check if user is currently on that spage
var id = ...; 
hub.client.NotifyUser = function(serverId, message) {

    if(id == serverId)
    {
        //do something with the message
    }
}