Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }