58

有没有办法找出侦听器的数量(连接到集线器的客户端?)

如果至少连接了一个客户端,我正在尝试运行/启动一项任务,否则不要启动它:

[HubName("taskActionStatus")]
public class TaskActionStatus : Hub, IDisconnect
{
    static CancellationTokenSource tokenSource;

    public void GetTasksStatus(int? siteId)
    {
        tokenSource = new CancellationTokenSource();
        CancellationToken ct = tokenSource.Token;

        ITaskRepository taskRepository = UnityContainerSetup.Container.Resolve<ITaskRepository>();

        // init task for checking task statuses
        var tasksItem = new DownloadTaskItem();
        taskRepository.GetTasksStatusAsync(siteId, tasksItem, ct);

        // subscribe to event [ listener ]
        tasksItem.Changed += new EventHandler<TaskEventArgs>(UpdateTasksStatus);
    }

    public void UpdateTasksStatus(object sender, TaskEventArgs e)
    {
        Clients.updateMessages(e.Tasks);
    }

    // when browsing away from page
    public Task Disconnect()
    {
        try
        {
            tokenSource.Cancel();
        }
        catch (Exception)
        {
            //
        }

        return null;
    }
}

谢谢

4

5 回答 5

108

没有办法从 SignalR 获得这个计数。您必须使用 Hub 上的OnConnect()OnDisconnect()方法来自己保持计数。

使用静态类保存计数的简单示例:

public static class UserHandler
{
    public static HashSet<string> ConnectedIds = new HashSet<string>();
}

public class MyHub : Hub
{
    public override Task OnConnectedAsync()
    {
        UserHandler.ConnectedIds.Add(Context.ConnectionId);
        return base.OnConnectedAsync();
    }

    public override Task OnDisconnectedAsync(Exception exception)
    {
        UserHandler.ConnectedIds.Remove(Context.ConnectionId);
        return base.OnDisconnectedAsync(exception);
    }
}

然后,您从 中获取计数UserHandler.ConnectedIds.Count

于 2012-11-22T14:01:17.470 回答
29

对于版本 2.1.1< 它应该是:

public static class UserHandler
{
    public static HashSet<string> ConnectedIds = new HashSet<string>();
}

public class MyHub : Hub
{
    public override Task OnConnected()
    {
        UserHandler.ConnectedIds.Add(Context.ConnectionId);
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        UserHandler.ConnectedIds.Remove(Context.ConnectionId);
        return base.OnDisconnected(stopCalled);
    }
}
于 2015-09-25T15:31:29.793 回答
7

在 SIGnalR(2.4.1 版)中,这相当容易:

public int GetOnline()
{
   return GlobalHost.DependencyResolver.Resolve<ITransportHeartbeat>().GetConnections().Count;
}

只需从客户端调用此方法(:

于 2019-07-24T08:46:05.550 回答
6

现在你需要:

public override Task OnConnectedAsync()
{
    UserHandler.ConnectedIds.Add(Context.ConnectionId);

    return base.OnConnectedAsync();
}

public override Task OnDisconnectedAsync(Exception exception)
{
    UserHandler.ConnectedIds.Remove(Context.ConnectionId);
    return base.OnDisconnectedAsync(exception);
}
于 2018-10-09T17:56:59.930 回答
1

我想补充一点,如果您使用 Azure Functions 和 Azure SignalR Service 的无服务器解决方案,则会出现以下已解决问题:https ://github.com/Azure/azure-functions-signalrservice-extension/issues/54

它指的是这个示例,您可以在其中使用事件网格来获取连接的实时计数,非常棒。https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/EventGridIntegration

于 2020-09-16T22:14:40.243 回答