Skip to content

Commit

Permalink
Use sample snippets for WPS (#20780)
Browse files Browse the repository at this point in the history
  • Loading branch information
tg-msft authored Apr 30, 2021
1 parent c917e3a commit 6f8da74
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
33 changes: 18 additions & 15 deletions sdk/webpubsub/Azure.Messaging.WebPubSub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>"));
```C# Snippet:WebPubSubAuthenticate
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
```

## Key concepts
Expand Down Expand Up @@ -69,32 +69,35 @@ 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("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>"));
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("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>"));
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
}));
```

### Broadcast a binary message to all clients

```csharp
var serviceClient = new WebPubSubServiceClient(new Uri("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>"));
```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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,30 @@ 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
}

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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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");
Expand Down

0 comments on commit 6f8da74

Please sign in to comment.