SignalR - 如何访问其他服务器端的用户映射 class 以向特定用户发送通知
SignalR - How to access user mapping in other server side class to send notifications to a particular user
我在我的 MVC 项目中实现了内存存储映射来存储 userId 及其相关的 connectionsId,如下面的映射 link 所示。问题是我无法访问另一服务器端的映射实例 class 并向特定用户发送通知。我应该如何修改 Hub 或 SendNotification class 来发送通知。请告诉我该怎么做。
\ here is the sample hub class with onconnected and disconnted same as in mapping link
public class NotificationHub : Hub
{
private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();
public void SendChatMessage(string who, string message)
{
string name = Context.User.Identity.Name;
foreach (var connectionId in _connections.GetConnections(who))
{
Clients.Client(connectionId).addChatMessage(name + ": " + message);
}
}
}
\ other server class where to call hub
public class SendNotification
{
Public void SaveToDb(string userName, class entity)
{
// save to database call
// send Notification to User userName through SignalR if user Exists in Mapping
}
}
这是一个 SignalR 映射 link (https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections)!
这是访问外部集线器的方法 (https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#callfromoutsidehub)!
您看过背板文档了吗?参见 https://docs.microsoft.com/en-us/aspnet/signalr/overview/performance/scaleout-in-signalr。
我们有一个包含许多 SignalR 客户端的多实例 Web 层。 Web 层中的任何实例都可以与任何浏览器客户端通信。 Hub 中的代码没有改变。启动时只有额外的逻辑——请参阅上面的文档。
我在我的 MVC 项目中实现了内存存储映射来存储 userId 及其相关的 connectionsId,如下面的映射 link 所示。问题是我无法访问另一服务器端的映射实例 class 并向特定用户发送通知。我应该如何修改 Hub 或 SendNotification class 来发送通知。请告诉我该怎么做。
\ here is the sample hub class with onconnected and disconnted same as in mapping link
public class NotificationHub : Hub
{
private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();
public void SendChatMessage(string who, string message)
{
string name = Context.User.Identity.Name;
foreach (var connectionId in _connections.GetConnections(who))
{
Clients.Client(connectionId).addChatMessage(name + ": " + message);
}
}
}
\ other server class where to call hub
public class SendNotification
{
Public void SaveToDb(string userName, class entity)
{
// save to database call
// send Notification to User userName through SignalR if user Exists in Mapping
}
}
这是一个 SignalR 映射 link (https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections)!
这是访问外部集线器的方法 (https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#callfromoutsidehub)!
您看过背板文档了吗?参见 https://docs.microsoft.com/en-us/aspnet/signalr/overview/performance/scaleout-in-signalr。
我们有一个包含许多 SignalR 客户端的多实例 Web 层。 Web 层中的任何实例都可以与任何浏览器客户端通信。 Hub 中的代码没有改变。启动时只有额外的逻辑——请参阅上面的文档。