【发布时间】:2022-04-01 17:35:22
【问题描述】:
有没有从控制器调用 SignalR hub 中方法的好方法?
现在我有这个:
public class StatsHub : Hub
{
private static readonly Lazy<StatsHub> instance = new Lazy<StatsHub>(() => new StatsHub());
public static StatsHub Instance { get { return instance.Value; } }
public StatsHub()
{
if (this.Clients == null)
{
var hubContext = SignalR.GlobalHost.ConnectionManager.GetHubContext<StatsHub>();
this.Clients = hubContext.Clients;
this.Groups = hubContext.Groups;
}
}
// methods here...
}
所以在我的控制器操作中我可以说,例如
StatsHub.Instance.SendMessage("blah");
它几乎是好的,除了 hubContext 没有 Hub 的 Caller 或 Context 属性——这些属性很好。
希望有更好的方法吗?
【问题讨论】:
标签: signalr