public async Task Run_for_a_while_using_WebSocket() { // Begin: README example var bayeuxClient = new BayeuxClient( new WebSocketTransportOptions() { Uri = new Uri("ws://localhost:8080/bayeux/"), }); // End: README example bayeuxClient.EventReceived += (e, args) => Debug.WriteLine($"Event received on channel {args.Channel} with data\n{args.Data}"); bayeuxClient.ConnectionStateChanged += (e, args) => Debug.WriteLine($"Bayeux connection state changed to {args.ConnectionState}"); await bayeuxClient.AddSubscriptionsAsync("/**"); await bayeuxClient.StartAsync(); using (bayeuxClient) { await Delay(60); } }
public async Task Reconnections() { var mock = new Mock <HttpMessageHandler>(); mock.Protected().As <IHttpMessageHandlerProtected>() .SetupSequence(h => h.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>())) .ReturnsAsync(BuildBayeuxResponse(successfulHandshakeResponse)) .ReturnsAsync(BuildBayeuxResponse(successfulConnectResponse)) .ReturnsAsync(BuildBayeuxResponse(rehandshakeConnectResponse)) .ThrowsAsync(new HttpRequestException("mock raising exception")) .ReturnsAsync(BuildBayeuxResponse(successfulHandshakeResponse)) .ThrowsAsync(new HttpRequestException("mock raising exception")) .ThrowsAsync(new HttpRequestException("mock raising exception")) .ThrowsAsync(new HttpRequestException("mock raising exception")) .ThrowsAsync(new HttpRequestException("mock raising exception")) .ThrowsAsync(new HttpRequestException("mock raising exception")) .ReturnsIndefinitely(() => Task.Delay(TimeSpan.FromSeconds(5)) .ContinueWith(t => BuildBayeuxResponse(successfulHandshakeResponse))) ; var bayeuxClient = new BayeuxClient( new HttpLongPollingTransportOptions() { HttpClient = new HttpClient(mock.Object), Uri = Url }, reconnectDelays: new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2) }); using (bayeuxClient) { bayeuxClient.StartAsync(); await Task.Delay(TimeSpan.FromSeconds(20)); } }
public async Task Run_for_a_while_using_HTTP() { // Begin: README example var httpClient = new HttpClient(); var bayeuxClient = new BayeuxClient( new HttpLongPollingTransportOptions() { HttpClient = httpClient, Uri = "http://localhost:8080/bayeux/", }); bayeuxClient.EventReceived += (e, args) => Debug.WriteLine($"Event received on channel {args.Channel} with data\n{args.Data}"); bayeuxClient.ConnectionStateChanged += (e, args) => Debug.WriteLine($"Bayeux connection state changed to {args.ConnectionState}"); await bayeuxClient.AddSubscriptionsAsync("/**"); await bayeuxClient.StartAsync(); // End: README example using (bayeuxClient) { await Delay(60); } }
public async Task Automatic_subscription() { var mock = new Mock <HttpMessageHandler>(); var mockProtected = mock.Protected().As <IHttpMessageHandlerProtected>(); int subscriptionCount = 0; mockProtected .Setup(h => h.SendAsync(MatchSubscriptionRequest(), It.IsAny <CancellationToken>())) .Returns(() => Task.Run(() => subscriptionCount++) .ContinueWith(t => BuildBayeuxResponse(successfulSubscriptionResponse))); mockProtected .Setup(h => h.SendAsync(MatchHandshakeRequest(), It.IsAny <CancellationToken>())) .ReturnsAsync(BuildBayeuxResponse(successfulHandshakeResponse)); mockProtected .Setup(h => h.SendAsync(MatchConnectRequest(), It.IsAny <CancellationToken>())) .Returns(() => Task.Delay(TimeSpan.FromSeconds(5)) .ContinueWith(t => BuildBayeuxResponse(successfulConnectResponse))); var bayeuxClient = new BayeuxClient( new HttpLongPollingTransportOptions() { HttpClient = new HttpClient(mock.Object), Uri = Url }, reconnectDelays: new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2) }); using (bayeuxClient) { await bayeuxClient.AddSubscriptionsAsync("/mychannel"); bayeuxClient.StartAsync(); await Task.Delay(TimeSpan.FromSeconds(2)); } Assert.AreEqual(1, subscriptionCount); }