Oli*_*old 2 c# signalr signalr-hub
在下面的代码示例中,我实现了一个应该实现以下功能的SignalR Hub:
使用单例作为集线器上下文是安全的还是我每次都需要在OnFooChanged中获取它?关于其他方法的实施的反馈也是受欢迎的.毕竟我是SignalR的新手.
[Export]
public class FooHub : Hub
{
private static readonly Lazy<IHubContext> ctx = new Lazy<IHubContext>(
() => GlobalHost.ConnectionManager.GetHubContext<FooHub>());
#region Client Methods
public void Subscribe(int[] fooIds)
{
foreach(var fooId in fooIds)
this.Groups.Add(this.Context.ConnectionId, fooId.ToString(CultureInfo.InvariantCulture));
}
public void Unsubscribe(int[] fooIds)
{
foreach (var fooId in fooIds)
this.Groups.Remove(this.Context.ConnectionId, fooId.ToString(CultureInfo.InvariantCulture));
}
#endregion // Client Methods
#region Server Methods
/// <summary>
/// Called from service layer when an instance of foo has changed
/// </summary>
public static void OnFooChanged(int id)
{
ctx.Value.Clients.Group(id.ToString(CultureInfo.InvariantCulture)).onFooChanged();
}
#endregion // Server Methods
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2742 次 |
| 最近记录: |