/// <summary>
 /// Create a service endpoint with endpoint and <see cref="AzureKeyCredential"/>.
 /// </summary>
 /// <param name="endpoint">The uri of target service endpoint.</param>
 /// <param name="credential">The <see cref="AzureKeyCredential"/>.</param>
 /// <param name="clientOptions">The <see cref="WebPubSubServiceClientOptions"/> to use when invoke service.</param>
 public ServiceEndpoint(Uri endpoint, AzureKeyCredential credential, WebPubSubServiceClientOptions clientOptions = null)
 {
     CredentialKind     = CredentialKind.AzureKeyCredential;
     Endpoint           = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
     AzureKeyCredential = credential ?? throw new ArgumentNullException(nameof(credential));
     ClientOptions      = clientOptions ?? new WebPubSubServiceClientOptions();
 }
 /// <summary>
 /// Create a service endpoint with connection string.
 /// </summary>
 /// <param name="connectionString">The service endpoint connection string.</param>
 /// <param name="clientOptions">The <see cref="WebPubSubServiceClientOptions"/> to use when invoke service.</param>
 public ServiceEndpoint(string connectionString, WebPubSubServiceClientOptions clientOptions = null)
 {
     CredentialKind        = CredentialKind.ConnectionString;
     ConnectionString      = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
     ClientOptions         = clientOptions ?? new WebPubSubServiceClientOptions();
     (Endpoint, AccessKey) = ParseConnectionString(connectionString);
 }
Example #3
0
        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);
        }
Example #4
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);
        }
        public void ReverseProxyEndpointRedirection()
        {
            var mockResponse = new MockResponse(202);
            var transport    = new MockTransport(mockResponse);

            var wpsEndpoint  = "https://wps.contoso.com/";
            var apimEndpoint = "https://apim.contoso.com/";
            var credentail   = new AzureKeyCredential("abcdabcdabcdabcdabcdabcdabcdabcd");

            var options = new WebPubSubServiceClientOptions();

            options.Transport            = transport;
            options.ReverseProxyEndpoint = new Uri(apimEndpoint);

            var client = new WebPubSubServiceClient(new Uri(wpsEndpoint), "test_hub", credentail, options);

            var response = client.SendToAll("Hello World!");

            Assert.AreEqual(202, response.Status);

            var requestUri = transport.SingleRequest.Uri.ToUri();

            Assert.AreEqual(new Uri(apimEndpoint).Host, requestUri.Host);
        }
Example #6
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);
        }
 internal WebPubSubServiceClient(Uri endpoint, AzureKeyCredential credential, WebPubSubServiceClientOptions options)
     : base(endpoint, typeof(THub).Name, credential, options)
 {
 }
 internal WebPubSubServiceClient(string connectionString, WebPubSubServiceClientOptions options)
     : base(connectionString, typeof(THub).Name, options)
 {
 }