Example #1
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;
     });
 }
        public async Task NegotiateInvokesGetNegotiationResponseOnTransportHelperAsync()
        {
            const string transportName = "fakeTransport";

            var connection = new Connection("http://fake.url/");
            var client = new DefaultHttpClient();
            var negotiationResponse = new NegotiationResponse();

            var mockTransportHelper = new Mock<TransportHelper>();
            mockTransportHelper.Setup(
                h => h.GetNegotiationResponse(It.IsAny<IHttpClient>(), It.IsAny<IConnection>(), It.IsAny<string>()))
                .Returns(Task.FromResult(negotiationResponse));

            var transport =
                new Mock<ClientTransportBase>(client, transportName, mockTransportHelper.Object,
                    new TransportAbortHandler(client, transportName))
                {
                    CallBase = true
                }.Object;

            Assert.Same(negotiationResponse, await transport.Negotiate(connection, "test"));

            mockTransportHelper.Verify(m => m.GetNegotiationResponse(client, connection, "test"), Times.Once());
        }
Example #3
0
 private static IClientTransport GetTransport(string transport)
 {
     if (!String.IsNullOrEmpty(transport))
     {
         var httpClient = new DefaultHttpClient();
         if (transport.Equals("WebSockets", StringComparison.InvariantCultureIgnoreCase))
         {
             return new WebSocketTransport(httpClient);
         }
         else if (transport.Equals("ServerSentEvents", StringComparison.InvariantCultureIgnoreCase))
         {
             return new ServerSentEventsTransport(httpClient);
         }
         else if (transport.Equals("LongPolling", StringComparison.InvariantCultureIgnoreCase))
         {
             return new LongPollingTransport(httpClient);
         }
         else if (transport.Equals("Auto", StringComparison.InvariantCultureIgnoreCase))
         {
             return new AutoTransport(httpClient);
         }
     }
     return null;
 }
Example #4
0
            public async Task FallbackToLongPollingIIS()
            {
                using (ITestHost host = CreateHost(HostType.IISExpress))
                {
                    // Reduce transportConnectionTimeout to 5 seconds
                    host.Initialize(transportConnectTimeout: 5);

                    var connection = CreateConnection(host, "/fall-back");

                    using (connection)
                    {
                        var tcs = new TaskCompletionSource<object>();

                        connection.StateChanged += change =>
                        {
                            if (change.NewState == ConnectionState.Reconnecting)
                            {
                                tcs.TrySetException(new Exception("The connection should not be reconnecting"));
                            }
                        };

                        var client = new DefaultHttpClient();
                        var transports = new IClientTransport[]  {
                            new ServerSentEventsTransport(client),
                            new LongPollingTransport(client)
                        };

                        var transport = new AutoTransport(client, transports);

                        await connection.Start(transport);

                        Assert.Equal(connection.Transport.Name, "longPolling");

                        Assert.False(tcs.Task.Wait(TimeSpan.FromSeconds(5)));
                    }
                }
            }
        private IClientTransport CreateTranport(string transportName)
        {
            TransportType transportType;
            if (!Enum.TryParse<TransportType>(transportName, true, out transportType))
            {
                // default it to Long Polling for transport
                transportType = TransportType.ServerSentEvents;
            }

            var client = new DefaultHttpClient();
            switch (transportType)
            {
                case TransportType.Websockets:
                    return new WebSocketTransport(client);
                case TransportType.ServerSentEvents:
                    return new ServerSentEventsTransport(client);
                case TransportType.ForeverFrame:
                    break;
                case TransportType.LongPolling:
                    return new LongPollingTransport(client);
                default:
                    return new AutoTransport(client);
            }

            throw new NotSupportedException("Transport not supported");

        }
Example #6
0
            public void FallbackToLongPollingIIS()
            {
                using (ITestHost host = new IISExpressTestHost())
                {
                    host.Initialize();

                    var connection = CreateConnection(host.Url + "/fall-back");
                    var tcs = new TaskCompletionSource<object>();

                    connection.StateChanged += change =>
                    {
                        if (change.NewState == ConnectionState.Reconnecting)
                        {
                            tcs.TrySetException(new Exception("The connection should not be reconnecting"));
                        }
                    };

                    var client = new DefaultHttpClient();
                    var transports = new IClientTransport[]  {
                        new ServerSentEventsTransport(client) { ConnectionTimeout = TimeSpan.Zero },
                        new LongPollingTransport(client)
                    };

                    var transport = new AutoTransport(client, transports);

                    connection.Start(transport).Wait();

                    Assert.Equal(connection.Transport.Name, "longPolling");

                    Assert.False(tcs.Task.Wait(TimeSpan.FromSeconds(5)));

                    connection.Stop();
                }
            }