signalR跟踪连接的用户 [英] signalR tracking connected users

查看:359
本文介绍了signalR跟踪连接的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查特定用户是否仍然连接。

I need to check whether a specific user is still connected.

我有以下HashSet的,用于跟踪用户的:

I have the following HashSet that keeps track of users:

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

于是就断开连接:

So on disconnected:

    public override Task OnDisconnected()
    {
        UserHandler.ConnectedIds.Remove(this.Context.ConnectionId);
        if (UserHandler.ConnectedIds.Count == 0)
        {
               // STOP TASK -> SET CANCELLATION
        }
        return base.OnDisconnected();
    }

和上连接:

    public override Task OnConnected()
    {
        Logger.Log.file.Info("[TaskActionStatus HUB => OnConnected] Start");

        UserHandler.ConnectedIds.Add(this.Context.ConnectionId);

        // start task only if at least one listener
        if (UserHandler.ConnectedIds.Count < 2)
        {
            // START A TASK -> (WHILE LOOP THAT GETS DATA FROM DB EVERY 2 SEC)
        }

        return base.OnConnected();
    }

现在,这个问题时,用户只需关闭他/她的浏览器窗口中出现。没有办法,我知道,如果他/她断开连接。

Now, the problem arises when a users simply closes his/her browser window. There is no way for me to know if he/she disconnected.

因此​​,基于一些问题,<一个href=\"http://stackoverflow.com/questions/9654588/signalr-for-tracking-online-users-and-chat\">here,我所说的每一个客户,并得到回应。我不知道该怎么做。

So based on some question here, I would call each client and get a response. I'm not sure how to do it.

我想我会有这样的:

public static void CheckConnectedUsers()
{
    var context = GlobalHost.ConnectionManager.GetHubContext<TaskActionStatus>();
    foreach (var connection in UserHandler.ConnectedIds)
    {
        context.Clients.Client(connection).verifyConnected("online?");
    }
}

但我怎么得到响应,形成客户端?

BUT how do I get a response back form the client?

编辑:
被执行的步骤:

steps that are executed:

我需要,因为连接的用户的确切数目如果有至少一个用户连接我需要开始一个任务。我想,以确保只有1任务运行所有连接的用户&LT; 2(启动一个任务)。假设这样的情况:2用户可以查看正在使用signalR的页面。 HashSet的现在包含2 IDS和任务正在运行。一关闭他/她的浏览器,并在几秒钟后,另一刷新他/她的。现在,第一个(即关闭了他/她的浏览器没有执行OnDisconnected方法,这样的HashSet仍然有他/她的ConnectionId的人,和一个刷新没有执行OnDisonnected然后OnConnected这增加了他的ConnectionId到HashSet的。因此,而不是在HashSet的仅具有1的ConnectionId和启动一个任务,HashSet的包含2(旧不再连接,并且只刷新他/她的浏览器中的一个),并且该任务从未被再次启动。

I need the exact number of users connected because If there is at least one user connected I need to start a task. And I want to ensure that only 1 task is running for all connected users < 2 (start a task). suppose a situation like this: 2 users view a page that is using signalR. HashSet now contains 2 ids and a task is running. One closes his/her browser and after a few seconds and another refreshes his/hers. Now, the first one (the one that closed his/her browser did not execute OnDisconnected method so HashSet still has his/her ConnectionId, and the one that "refreshed" did execute OnDisonnected and then OnConnected which added his connectionId to HashSet. So instead of having only 1 ConnectionId in a HashSet and starting a task, HashSet contains 2 (old-not connected anymore and the one that just refreshed his/her browser), and the task never got started again.

感谢

推荐答案

通过SignalR 1.0, OnDisconnected 要经常起火,即使用户关闭其窗口(例外是如果服务器重新启动)。

With SignalR 1.0, OnDisconnected should always fire, even when users close their window (the exception to this being if the server restarts).

OnDisconnected 可能不会立即解雇,因为如果客户端不流产,它被认为是不干净的连接,因为有机会在客户端会尝试重新连接(如果你重新连接你有没有按照SignalR的语义断开)。

OnDisconnected may not fire immediately, because if the client doesn't "abort" the connection it is considered "unclean" since there is a chance the client may try and reconnect (and if you reconnect you haven't disconnected according to SignalR's semantics).

在默认情况下,它应该需要30秒左右的服务器调用 OnDisconnected 为不干净的脱节。这可以通过 GlobalHost.Configuration.DisconnectTimeout 进行重新配置。

By default, it should take about 30 seconds for the server to call OnDisconnected for an "unclean" disconnect. This can be reconfigured via GlobalHost.Configuration.DisconnectTimeout.

这篇关于signalR跟踪连接的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆