Inheritance: Microsoft.AspNet.SignalR.Client.Transports.HttpBasedTransport
Beispiel #1
0
        public void CancelledTaskHandledinServerSentEvents()
        {
            var tcs = new TaskCompletionSource<IResponse>();
            tcs.TrySetCanceled();

            var httpClient = new Mock<IHttpClient>();
            var connection = new Mock<IConnection>();

            httpClient.Setup(c => c.Get(It.IsAny<string>(), It.IsAny<Action<IRequest>>(), It.IsAny<bool>()))
                .Returns<string, Action<IRequest>, bool>((url, prepareRequest, isLongRunning) =>
                {
                    prepareRequest(Mock.Of<IRequest>());
                    return tcs.Task;
                });

            connection.SetupGet(c => c.ConnectionToken).Returns("foo");
            connection.SetupGet(c => c.TotalTransportConnectTimeout).Returns(TimeSpan.FromSeconds(15));

            var sse = new ServerSentEventsTransport(httpClient.Object);

            var exception = Assert.Throws<AggregateException>(
                () => sse.Start(connection.Object, string.Empty, CancellationToken.None).Wait(TimeSpan.FromSeconds(5)));

            Assert.IsType(typeof(OperationCanceledException), exception.InnerException);
        }
Beispiel #2
0
        public void CancelledTaskHandledinServerSentEvents()
        {
            var tcs = new TaskCompletionSource<IResponse>();
            var wh = new TaskCompletionSource<Exception>();

            tcs.TrySetCanceled();

            var httpClient = new Mock<Microsoft.AspNet.SignalR.Client.Http.IHttpClient>();
            var connection = new Mock<Microsoft.AspNet.SignalR.Client.IConnection>();

            httpClient.Setup(c => c.Get(It.IsAny<string>(),
                It.IsAny<Action<Client.Http.IRequest>>(), It.IsAny<bool>()))
                .Returns(tcs.Task);

            connection.SetupGet(c => c.ConnectionToken).Returns("foo");

            var sse = new ServerSentEventsTransport(httpClient.Object);
            sse.OpenConnection(connection.Object, (ex) =>
            {
                wh.TrySetResult(ex);
            });

            Assert.True(wh.Task.Wait(TimeSpan.FromSeconds(5)));
            Assert.IsType(typeof(OperationCanceledException), wh.Task.Result);
        }
 public Task Start()
 {
     var serverSentEventsTransport = new ServerSentEventsTransport();
     return _wrapped.Start(serverSentEventsTransport);
 }
Beispiel #4
0
        private static void StartListen(string hubName, string baseUrl, Action callback)
        {
            connection = new HubConnection(baseUrl);

            connection.Credentials = System.Net.CredentialCache.DefaultCredentials;

            var cc = new System.Net.CookieContainer();
            var cookiename = "userinfo";
            var cookievalue =
                @"3kiJ2IdvqPGaSuxB7J8Kk9l7nwFTV0mMB0gqTY5pLKePBukJ/Onh9fBBGHxWwKDxkTZfLsebSK1QgqRNmsYNrjkYriErk7N0ESyOH426P+ddVfNjYLYF39M22WG0nR/X1ZfHB2AphqnEzlPryVoirqOZa9c5ti7FC55S7O21myqOgJZigkozRj6+65SsdbfG";
            cc.Add(new Uri(baseUrl), new System.Net.Cookie(cookiename, cookievalue));

            connection.CookieContainer =
                Domas.DAP.ADF.Cookie.CookieManger.GetUriCookieContainer(new Uri(baseUrl));

            var hubProxy = connection.CreateHubProxy(hubName);

            Action reConnection = () =>
            {
                try
                {
                    lock (connection)
                    {
                        connection.Stop();
                    }
                }
                finally
                {
                    while (true)
                    {
                        Thread.Sleep(1000);
                        if (connection == null || connection.State == ConnectionState.Disconnected)
                        {

                            break;
                        }
                    }
                    StartListen(hubName, baseUrl, callback);
                }
            };
            hubProxy.On("addMessage", () => Task.Factory.StartNew(() =>
            {
                hubProxy.Invoke("ServerCallback").ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {

                    }
                    else
                    {
                        callback();
                    }
                });
            }));
            hubProxy.On("CloseClient", () =>
            {
                reConnection();
            });
            var sseTransport = new ServerSentEventsTransport();

            connection.Start(sseTransport).ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    throw task.Exception;
                }
                else
                {
                    Console.WriteLine("connected.");
                    connection.Reconnected += () =>
                    {
                        reConnection();
                    };
                }
            }).Wait();


            hubProxy.Invoke("SetClientAgent", "Notifier").ContinueWith(task =>
            {
                if (task.IsFaulted)
                {

                }
                else
                {
                    Console.WriteLine("first invoke success.");
                }
            }).Wait();
        }