Beispiel #1
0
        public void Run()
        {
            if (_timer != null && _timer.Enabled)
            {
                return;
            }

            var url = _hubConfiguration.GetHubContainingSiteUrl();

            try
            {
                if (_connection == null)
                {
                    // connect to the hub
                    _connection = new HubConnection(url);

                    // create a proxy
                    _hub = _connection.CreateProxy("siteMonitorNotificationHub");

                    // whenever a site is added
                    _hub.On <string>("siteAdded", (siteUrl) =>
                    {
                        _siteRepository.Add(siteUrl);
                        _hub.Invoke("addSiteToGui", siteUrl);
                    });

                    // whenever a site is removed
                    _hub.On <string>("siteRemoved", (siteUrl) =>
                    {
                        _siteRepository.Remove(siteUrl);
                        _hub.Invoke("removeSiteFromGui", siteUrl);
                    });

                    // now start the connection
                    _connection.Start().Wait();
                }

                _timer = new Timer(5000);

                _timer.Elapsed += (s, e) =>
                {
                    _timer.Stop();

                    _siteRepository.GetUrls()
                    .ForEach(x =>
                    {
                        var result = false;

                        try
                        {
                            var output = new WebClient().DownloadString(x);
                            result     = true;
                            Log(x + " is up");
                        }
                        catch (Exception ex)
                        {
                            result = false;
                            Log(x + " is down");
                        }

                        // invoke a method on the hub
                        _hub.Invoke("ReceiveMonitorUpdate", new
                        {
                            Url    = x,
                            Result = result
                        });
                    });

                    _timer.Start();

                    Log("All sites pinged. Sleeping...");
                    System.Threading.Thread.Sleep(3000);
                };

                _timer.Start();
            }
            catch (Exception x)
            {
                if (_timer != null)
                {
                    _timer.Stop();
                }
            }
        }
 public void RemoveSite(string url)
 {
     _repository.Remove(url);
     Clients.All.siteRemovedFromGui(url);
 }