private async Task ConnectAndWaitForMessages(string userId, ConcurrentBag <string> usersThatRecievedMessages, ManualResetEvent listeningStartedEvent, ManualResetEvent finishedListeningEvent) { ManualResetEventSlim resetEvent = new ManualResetEventSlim(false); SignalRToken token = await Settings.Host .AppendPathSegment("negotiate") .SetQueryParam("userId", userId) .GetJsonAsync <SignalRToken>(); HubConnection connection = new HubConnectionBuilder() .WithUrl(token.Url, options => options.AccessTokenProvider = () => Task.FromResult(token.AccessToken)) .Build(); connection.On <Guid>("sendMessageCollectionCommand", (markerId) => { resetEvent.Set(); }); await connection.StartAsync(); listeningStartedEvent.Set(); bool didReceive = resetEvent.Wait(TimeSpan.FromSeconds(10)); if (didReceive) { usersThatRecievedMessages.Add(userId); } finishedListeningEvent.Set(); }
public async Task AuthenticateWithBindingExpressionNegotiateAndSendAndRecieveMessage() { ManualResetEventSlim resetEvent = new ManualResetEventSlim(false); Guid markerId = Guid.NewGuid(); string receivedMessage = null; SignalRToken token = await Settings.Host .AppendPathSegment("simpleNegotiate") .WithHeaders(new { x_ms_client_principal_id = Guid.NewGuid() }) .GetJsonAsync <SignalRToken>(); Assert.NotNull(token); HubConnection connection = new HubConnectionBuilder() .WithUrl(token.Url, options => options.AccessTokenProvider = () => Task.FromResult(token.AccessToken)) .Build(); connection.On <string>("sendMessageCommand", (message) => { receivedMessage = message; resetEvent.Set(); }); await connection.StartAsync(); await Settings.Host .AppendPathSegment("signalR") .AppendPathSegment("messageToAll") .SetQueryParam("message", markerId.ToString()) .GetAsync(); bool didReceive = resetEvent.Wait(TimeSpan.FromSeconds(30)); Assert.True(didReceive); Assert.Equal(markerId.ToString(), receivedMessage); }
public async Task CanRecieveMessageAfterAddingToGroupButNotAfterRemoving() { ManualResetEventSlim resetEvent = new ManualResetEventSlim(false); string userId = Guid.NewGuid().ToString(); await Settings.Host .AppendPathSegment("signalR") .AppendPathSegment("addUserToGroup") .PutJsonAsync(new { UserId = userId, GroupName = "group1" }); SignalRToken token = await Settings.Host .AppendPathSegment("negotiate") .SetQueryParam("userId", userId) .GetJsonAsync <SignalRToken>(); HubConnection connection = new HubConnectionBuilder() .WithUrl(token.Url, options => options.AccessTokenProvider = () => Task.FromResult(token.AccessToken)) .Build(); connection.On <string>("sendMessageToGroupCommand", (message) => { resetEvent.Set(); }); await connection.StartAsync(); await Settings.Host .AppendPathSegment("signalR") .AppendPathSegment("messageToGroup") .AppendPathSegment("group1") .SetQueryParam("userId", userId) .GetAsync(); bool didReceive = resetEvent.Wait(TimeSpan.FromSeconds(5)); Assert.True(didReceive); resetEvent.Reset(); await Settings.Host .AppendPathSegment("signalR") .AppendPathSegment("removeUserFromGroup") .PutJsonAsync(new { UserId = userId, GroupName = "group1" }); await Settings.Host .AppendPathSegment("signalR") .AppendPathSegment("messageToGroup") .AppendPathSegment("group1") .SetQueryParam("userId", userId) .GetAsync(); didReceive = resetEvent.Wait(TimeSpan.FromSeconds(5)); Assert.False(didReceive); }
public async Task AuthenticateSendAndRecieveMessagesForUser1234() { Guid[] markerIds = { Guid.NewGuid(), Guid.NewGuid() }; ConcurrentBag <Guid> receivedGuids = new ConcurrentBag <Guid>(); SignalRToken token = await Settings.Host .AppendPathSegment("negotiate") .SetQueryParam("userId", "1234") .GetJsonAsync <SignalRToken>(); Assert.NotNull(token); HubConnection connection = new HubConnectionBuilder() .WithUrl(token.Url, options => options.AccessTokenProvider = () => Task.FromResult(token.AccessToken)) .Build(); connection.On <Guid>("sendMessageCollectionCommand", (markerId) => { receivedGuids.Add(markerId); }); await connection.StartAsync(); await Settings.Host .AppendPathSegment("signalR") .AppendPathSegment("messageCollectionToUser") .SetQueryParam("message", markerIds.ToString()) .PostJsonAsync(new { userId = "1234", markerIds }); double timeTaken = 0; while (timeTaken < 20000 && receivedGuids.Count < 2) { await Task.Delay(TimeSpan.FromMilliseconds(500)); timeTaken += 500; } Assert.Contains(markerIds[0], receivedGuids); Assert.Contains(markerIds[1], receivedGuids); }