public async Task ServiceClientCanBroadcastMessages() { WebPubSubServiceClientOptions options = InstrumentClientOptions(new WebPubSubServiceClientOptions()); var serviceClient = new WebPubSubServiceClient(TestEnvironment.ConnectionString, nameof(ServiceClientCanBroadcastMessages), options); // broadcast messages var textContent = "Hello"; var response = await serviceClient.SendToAllAsync(textContent, ContentType.TextPlain); Assert.AreEqual(202, response.Status); var jsonContent = BinaryData.FromObjectAsJson(new { hello = "world" }); response = await serviceClient.SendToAllAsync(RequestContent.Create(jsonContent), ContentType.ApplicationJson); Assert.AreEqual(202, response.Status); var binaryContent = BinaryData.FromString("Hello"); response = await serviceClient.SendToAllAsync(RequestContent.Create(binaryContent), ContentType.ApplicationOctetStream); Assert.AreEqual(202, response.Status); }
public async Task SimpleWebSocketClientCanConnectAndReceiveMessage() { WebPubSubServiceClientOptions options = InstrumentClientOptions(new WebPubSubServiceClientOptions()); var serviceClient = new WebPubSubServiceClient(TestEnvironment.ConnectionString, nameof(SimpleWebSocketClientCanConnectAndReceiveMessage), options); var url = await serviceClient.GetClientAccessUriAsync(); // start the connection using var client = new WebSocketClient(url, IsSimpleClientEndSignal); // connected await client.WaitForConnected.OrTimeout(); // broadcast messages var textContent = "Hello"; await serviceClient.SendToAllAsync(textContent, ContentType.TextPlain); var jsonContent = BinaryData.FromObjectAsJson(new { hello = "world" }); await serviceClient.SendToAllAsync(RequestContent.Create(jsonContent), ContentType.ApplicationJson); var binaryContent = BinaryData.FromString("Hello"); await serviceClient.SendToAllAsync(RequestContent.Create(binaryContent), ContentType.ApplicationOctetStream); await serviceClient.SendToAllAsync(RequestContent.Create(GetEndSignalBytes()), ContentType.ApplicationOctetStream); await client.LifetimeTask.OrTimeout(); var frames = client.ReceivedFrames; Assert.AreEqual(3, frames.Count); Assert.AreEqual(textContent, frames[0].MessageAsString); Assert.AreEqual(jsonContent.ToString(), frames[1].MessageAsString); CollectionAssert.AreEquivalent(binaryContent.ToArray(), frames[2].MessageBytes); }
public async Task SendToAll(SendToAll webPubSubEvent) { var content = RequestContent.Create(webPubSubEvent.Message); var contentType = Utilities.GetContentType(webPubSubEvent.DataType); await _client.SendToAllAsync(content, contentType, webPubSubEvent.Excluded).ConfigureAwait(false); }
public async Task SubprotocolWebSocketClientCanConnectAndReceiveMessage() { WebPubSubServiceClientOptions options = InstrumentClientOptions(new WebPubSubServiceClientOptions()); var serviceClient = new WebPubSubServiceClient(TestEnvironment.ConnectionString, nameof(SubprotocolWebSocketClientCanConnectAndReceiveMessage), options); var url = await serviceClient.GetClientAccessUriAsync(); // start the connection using var client = new WebSocketClient(url, IsSubprotocolClientEndSignal, a => a.AddSubProtocol("json.webpubsub.azure.v1")); // connected await client.WaitForConnected.OrTimeout(); // broadcast messages var textContent = "Hello"; await serviceClient.SendToAllAsync(textContent, ContentType.TextPlain); var jsonContent = new { hello = "world" }; await serviceClient.SendToAllAsync(RequestContent.Create(BinaryData.FromObjectAsJson(jsonContent)), ContentType.ApplicationJson); var binaryContent = BinaryData.FromString("Hello"); await serviceClient.SendToAllAsync(RequestContent.Create(binaryContent), ContentType.ApplicationOctetStream); await serviceClient.SendToAllAsync(RequestContent.Create(GetEndSignalBytes()), ContentType.ApplicationOctetStream); await client.LifetimeTask.OrTimeout(); var frames = client.ReceivedFrames; Assert.AreEqual(4, frames.Count); // first message must be the "connected" message var connected = JsonSerializer.Deserialize <ConnectedMessage>(frames[0].MessageAsString, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); Assert.NotNull(connected); Assert.AreEqual("connected", connected.Event); Assert.AreEqual(JsonSerializer.Serialize(new { type = "message", from = "server", dataType = "text", data = textContent }), frames[1].MessageAsString); Assert.AreEqual(JsonSerializer.Serialize(new { type = "message", from = "server", dataType = "json", data = jsonContent }), frames[2].MessageAsString); CollectionAssert.AreEquivalent(JsonSerializer.Serialize(new { type = "message", from = "server", dataType = "binary", data = Convert.ToBase64String(binaryContent.ToArray()) }), frames[3].MessageBytes); }