Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 信号器-连接客户端时循环_C#_.net_Asp.net Mvc 4_Signalr - Fatal编程技术网

C# 信号器-连接客户端时循环

C# 信号器-连接客户端时循环,c#,.net,asp.net-mvc-4,signalr,C#,.net,Asp.net Mvc 4,Signalr,我最近一直在玩signar,发现它对于服务器/客户机通信非常棒 然后,我想到了一个与我目前正在进行的项目非常相关的场景 其基本思想是通过ping机器在网页上指示服务器当前是否在线 该过程由第一个访问页面的用户启动,当所有客户端都断开连接时,该过程应停止 到目前为止,我的服务器代码如下: public class PingHub : Hub { private static readonly ConcurrentDictionary<string, DateTime> _use

我最近一直在玩signar,发现它对于服务器/客户机通信非常棒

然后,我想到了一个与我目前正在进行的项目非常相关的场景

其基本思想是通过ping机器在网页上指示服务器当前是否在线

该过程由第一个访问页面的用户启动,当所有客户端都断开连接时,该过程应停止

到目前为止,我的服务器代码如下:

public class PingHub : Hub
{
    private static readonly ConcurrentDictionary<string, DateTime> _users = new ConcurrentDictionary<string, DateTime>();
    private static bool _isPolling = false;

    public override Task OnConnected()
    {
        _users.TryAdd(Context.ConnectionId, DateTime.Now);

        return Clients.All.UserCountChanged(_users.Count);
    }

    public override Task OnReconnected()
    {
        _users.AddOrUpdate(Context.ConnectionId, DateTime.Now, (key, value) => value);

        return Clients.All.UserCountChanged(_users.Count);
    }

    public override Task OnDisconnected()
    {
        DateTime value;
        _users.TryRemove(Context.ConnectionId, out value);

        return Clients.All.UserCountChanged(_users.Count);
    }

    public void GetStatus()
    {
        if (_isPolling)
            return;

        _isPolling = true;

        var ping = new Ping();
        do
        {
            var reply = ping.Send("X.X.X.X");

            if (reply == null)
            {
                Clients.Caller.SetError(new { error = "No reply!" });
                break;
            }

            Clients.All.SetStatus(new { Status = reply.Status.ToString(), Time = reply.RoundtripTime });

            Thread.Sleep(500);
        } while (_users.Count > 0);

        _isPolling = false;
    }
}
问题是,启动进程的用户保持连接,即使浏览器关闭,我通过记录每个ping请求来测试这一点


那么有谁能帮我把这项工作做好吗?

我最近也做了类似的事情。在我的Global.asax.cs中,我有:

private BackgroundProcess bp;
protected void Application_Start( object sender, EventArgs e )
{
    ...
    bp = new BackgroundProcess();
}
然后我有一个BackgroundProcess.cs:

public class BackgroundProcess : IRegisteredObject
{
    private Timer TaskTimer;
    private IHubContext ClientHub;

    public BackgroundProcess()
    {
        HostingEnvironment.RegisterObject( this );

        ClientHub = GlobalHost.ConnectionManager.GetHubContext<webClientHub>();

        TaskTimer = new Timer( OnTimerElapsed, null, TimeSpan.FromSeconds( 0 ), TimeSpan.FromSeconds( 10 ) );
    }

    private void OnTimerElapsed( object sender )
    {
        if (...) //HasAnyClientsConnected, not sure what this would be, I had other criteria
        {
            ClientHub.Clients.All.ping(); //Do your own ping
        }
    }

    public void Stop( bool immediate )
    {
        TaskTimer.Dispose();
        HostingEnvironment.UnregisterObject( this );
    }
这里的关键是IRegisteredObject。您可以查找它,但web应用程序会在它因任何原因关闭时通知它