Ejemplo n.º 1
0
 private static void RegisterForNotifications()
 {
     try
     {
         var userName = GetConfigServiceUser();
         var password = GetConfigServicePassword();
         GlobalHost.HubPipeline.RequireAuthentication();
         hubConnection = new HubConnection(GetConfigLocation());
         if (hubConnection.CookieContainer == null) hubConnection.CookieContainer = new CookieContainer();
         //hubConnection.Credentials = new NetworkCredential(userName, password, GetConfigServiceDomain());
         ConfigCacheHelper.SetCredentials(hubConnection);
         hub = hubConnection.CreateHubProxy("configSetHub");
         hub.On("changed",
             (string id, string name) =>
             {
                 if (string.Equals(id, "user", StringComparison.OrdinalIgnoreCase))
                 {
                     UpdateUser(name);
                 }
                 else
                 {
                     UpdateEnvironmet(id, name);
                 }
             });
         hubConnection.EnsureReconnecting();
         hubConnection.Reconnecting += hubConnection_Reconnecting;
         hubConnection.Closed += hubConnection_Closed;
         hubConnection.Error += hubConnection_Error;
         hubConnection.StateChanged += hubConnection_StateChanged;
         hubConnection.Start();
         CreatePingService();
     }
     catch (Exception ex)
     {
         ex.Log();
     }
 }
Ejemplo n.º 2
0
 private static Task Do()
 {
     string url = "http://192.168.70.118:1980";
     var connection = new HubConnection(url);
     IHubProxy hub = connection.CreateHubProxy("echo");
     var httpClient = new DefaultHttpClient();
     var transport = new AutoTransport(
         httpClient,
         new IClientTransport[]
         {
             //new ServerSentEventsTransport(httpClient),
             new LongPollingTransport(httpClient)
         }
         );
     //connection.Error +=
     //    error => ConsoleColor.Red.AsColorFor(() => Console.WriteLine("Error from connection: {0}", error));
     connection.Closed += () =>
     {
         Console.WriteLine("Closed");
         //if (!connection.EnsureReconnecting())
         //{
         //    Task.Delay(TimeSpan.FromSeconds(30)).ContinueWith(t => connection.Start().Wait());
         //}
         if (!connection.EnsureReconnecting())
         {
             Task.Factory.StartNew(() => Thread.Sleep(TimeSpan.FromSeconds(30)))
                 .ContinueWith(t => connection.Start().Wait());
         }
     };
     connection.ConnectionSlow += () => Console.WriteLine("ConnectionSolw!");
     connection.Received += data => Console.WriteLine(string.Format("Received:{0}", data));
     connection.Reconnected += () => Console.WriteLine("Reconnected!");
     connection.StateChanged +=
         state =>
             Console.WriteLine("StateChanged:From {0} to {1}", state.OldState, state.NewState);
     return connection.Start(transport).ContinueWith(_ =>
     {
         Console.WriteLine("Connected, transport is :{0}", connection.Transport.Name);
         return hub;
     });
 }