我正在为我的聊天应用程序使用 SingalR。想玩 Redis 和 SignalR,但我找不到可以将 msg 发送到特定 connectionId 的工作示例。下面的代码适用于单个服务器实例。但是,当我将其设置为具有 3 个进程的 Web Garden 时,它会停止工作,因为获取消息的服务器实例无法找到该目标 UserId 的 connectionId 以发送消息。
private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();
public void Send(string sendTo, string message, string from)
{
string fromclientid = Context.QueryString["clientid"];
foreach (var connectionId in _connections.GetConnections(sendTo))
{
Clients.Client(connectionId).send(fromclientid, message);
}
Clients.Caller.send(sendTo, "me: " + message);
}
public override Task OnConnected()
{
int clientid = Convert.ToInt32(Context.QueryString["clientid"]);
_connections.Add(clientid.ToString(), Context.ConnectionId);
}
我已经使用下面的示例来设置我的盒子和代码,但它们都没有从一个客户端发送到特定客户端或一组特定客户端的示例。
http://www.asp.net/signalr/overview/performance-and-scaling/scaleout-with-redis
https://github.com/mickdelaney/SignalR.Redis/tree/master/Redis.Sample