我使用Redis
作为背板,但我也使用SQL将用户映射到连接。这会对我的I / O产生影响 - 每次我通过集线器发送消息时,我都会这样做:
private readonly ConnectionService _connectionService;
public AppHub() {
_connectionService = new ConnectionService();
}
public void SendClientNotification(string notification, string key) {
List<string> navbarClients = _connectionService.GetConnections(key); // yuck
foreach (string connectionId in navbarClients) {
Clients.Client(connectionId).clientNotification(notification);
}
}
GetConnections
是这样的:
public List<string> GetConnections(string key) {
using (DbContext db = new DbContext()) {
return db.SignalConnections
.Where(s => s.Key == key)
.Select(s => s.ConnectionGuid)
.ToList();
}
}
我还会做一大堆I / O来定期删除非活动连接。
SignalR能否以某种方式推断用户名(例如来自User.Identity.Name
)并自动处理映射和连接到期,以便我可以从SignalR实现中消除这种SQL依赖性?