From 6f8da74935368091580733334efdfaf5ede42a99 Mon Sep 17 00:00:00 2001 From: tg-msft Date: Fri, 30 Apr 2021 10:56:08 -0700 Subject: [PATCH] Use sample snippets for WPS (#20780) --- .../Azure.Messaging.WebPubSub/README.md | 33 +++++++------ .../Samples/WebPubSubSamples.HelloWorld.cs | 48 ++++++++++++------- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/README.md b/sdk/webpubsub/Azure.Messaging.WebPubSub/README.md index 65782951dc411..0aa2a29a1b357 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/README.md +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/README.md @@ -39,8 +39,8 @@ In order to interact with the service, you'll need to create an instance of the ### Create a `WebPubSubServiceClient` -```csharp -var serviceClient = new WebPubSubServiceClient(new Uri(""), "", new AzureKeyCredential("")); +```C# Snippet:WebPubSubAuthenticate +var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); ``` ## Key concepts @@ -69,18 +69,21 @@ Using this library, you can send messages to the client connections. A message c ### Broadcast a text message to all clients -```csharp -var serviceClient = new WebPubSubServiceClient(new Uri(""), "", new AzureKeyCredential("")); -await serviceClient.SendToAll("Hello world!"); +```C# Snippet:WebPubSubHelloWorld +var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + +serviceClient.SendToAll("Hello World!"); ``` ### Broadcast a JSON message to all clients -```csharp -var serviceClient = new WebPubSubServiceClient(new Uri(""), "", new AzureKeyCredential("")); -await serviceClient.SendToAll( +```C# Snippet:WebPubSubSendJson +var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + +serviceClient.SendToAll( RequestContent.Create( - new { + new + { Foo = "Hello World!", Bar = 42 })); @@ -88,13 +91,13 @@ await serviceClient.SendToAll( ### Broadcast a binary message to all clients -```csharp -var serviceClient = new WebPubSubServiceClient(new Uri(""), "", new AzureKeyCredential("")); +```C# Snippet:WebPubSubSendBinary +var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); -client.SendToAll( - RequestContent.Create(new byte[] {0x1, 0x2, 0x3}), - HttpHeader.Common.OctetStreamContentType.Value -); +Stream stream = BinaryData.FromString("Hello World!").ToStream(); +serviceClient.SendToAll( + RequestContent.Create(stream), + HttpHeader.Common.OctetStreamContentType.Value); ``` ## Troubleshooting diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs index e4dd5da1f19ca..958a5098d03ff 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs @@ -20,9 +20,19 @@ public void HelloWorld() var key = TestEnvironment.Key; #region Snippet:WebPubSubHelloWorld - var client = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + + serviceClient.SendToAll("Hello World!"); + #endregion + } + + public void Authenticate() + { + var endpoint = TestEnvironment.Endpoint; + var key = TestEnvironment.Key; - client.SendToAll("Hello World!"); + #region Snippet:WebPubSubAuthenticate + var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); #endregion } @@ -30,10 +40,10 @@ public void HelloWorldWithConnectionString() { var connectionString = TestEnvironment.ConnectionString; - #region Snippet:WebPubSubHelloWorld - var client = new WebPubSubServiceClient(connectionString, "some_hub"); + #region Snippet:WebPubSubHelloWorldConnStr + var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub"); - client.SendToAll("Hello World!"); + serviceClient.SendToAll("Hello World!"); #endregion } @@ -43,14 +53,15 @@ public void JsonMessage() var key = TestEnvironment.Key; #region Snippet:WebPubSubSendJson - var client = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); - - client.SendToAll(RequestContent.Create( - new { - Foo = "Hello World!", - Bar = "Hi!" - } - )); + var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + + serviceClient.SendToAll( + RequestContent.Create( + new + { + Foo = "Hello World!", + Bar = 42 + })); #endregion } @@ -59,12 +70,13 @@ public void BinaryMessage() var endpoint = TestEnvironment.Endpoint; var key = TestEnvironment.Key; - #region Snippet:WebPubSubSendJson - var client = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + #region Snippet:WebPubSubSendBinary + var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); Stream stream = BinaryData.FromString("Hello World!").ToStream(); - - client.SendToAll(RequestContent.Create(stream), HttpHeader.Common.OctetStreamContentType.Value); + serviceClient.SendToAll( + RequestContent.Create(stream), + HttpHeader.Common.OctetStreamContentType.Value); #endregion } @@ -73,7 +85,7 @@ public void AddUserToGroup() var endpoint = TestEnvironment.Endpoint; var key = TestEnvironment.Key; - #region Snippet:WebPubSubSendJson + #region Snippet:WebPubAddUserToGroup var client = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); client.AddUserToGroup("some_group", "some_user");