From bc19c2c8bf13733866b6babd3ef90d8d02715860 Mon Sep 17 00:00:00 2001 From: Chenyang Liu Date: Sun, 29 Jan 2023 14:57:20 +0800 Subject: [PATCH 1/2] Add pubsub client sdk sample --- .../csharp/clientpubsub-client-sdk/Readme.md | 26 +++++++++++ .../clientpub/Program.cs | 46 +++++++++++++++++++ .../clientpub/clientpub.csproj | 13 ++++++ .../clientsub/Program.cs | 46 +++++++++++++++++++ .../clientsub/clientsub.csproj | 13 ++++++ 5 files changed, 144 insertions(+) create mode 100644 samples/csharp/clientpubsub-client-sdk/Readme.md create mode 100644 samples/csharp/clientpubsub-client-sdk/clientpub/Program.cs create mode 100644 samples/csharp/clientpubsub-client-sdk/clientpub/clientpub.csproj create mode 100644 samples/csharp/clientpubsub-client-sdk/clientsub/Program.cs create mode 100644 samples/csharp/clientpubsub-client-sdk/clientsub/clientsub.csproj diff --git a/samples/csharp/clientpubsub-client-sdk/Readme.md b/samples/csharp/clientpubsub-client-sdk/Readme.md new file mode 100644 index 000000000..34e7ffaa4 --- /dev/null +++ b/samples/csharp/clientpubsub-client-sdk/Readme.md @@ -0,0 +1,26 @@ +# Client pub-sub with `Azure.Messaging.WebPubSub.Client` + +## Prerequisites + +1. [NET 6 or above](https://docs.microsoft.com/dotnet) +2. Create an Azure Web PubSub resource + +## Setup + +Copy **Connection String** from **Keys** tab of the created Azure Web PubSub service, and replace the `` below with the value of your **Connection String**. + +![Connection String](./../../../docs/images/portal_conn.png) + +1. Start Client subscriber +```bash +cd clientsub +dotnet run -- "" pubsubhub +``` + +2. Start Client publisher +```bash +cd clientpub +dotnet run -- "ConnectionString>" pubsubhub +``` + +Start typing messages and you can see these messages are transferred to the client subscriber in real-time. diff --git a/samples/csharp/clientpubsub-client-sdk/clientpub/Program.cs b/samples/csharp/clientpubsub-client-sdk/clientpub/Program.cs new file mode 100644 index 000000000..2db4bd9de --- /dev/null +++ b/samples/csharp/clientpubsub-client-sdk/clientpub/Program.cs @@ -0,0 +1,46 @@ +using System; +using System.IO; +using System.Threading.Tasks; + +using Azure.Messaging.WebPubSub; +using Azure.Messaging.WebPubSub.Clients; + +namespace clientpub +{ + class Program + { + static async Task Main(string[] args) + { + if (args.Length != 2) + { + Console.WriteLine("Usage: clientpub "); + return; + } + var connectionString = args[0]; + var hub = args[1]; + + // Either generate the URL or fetch it from server or fetch a temp one from the portal + var serviceClient = new WebPubSubServiceClient(connectionString, hub); + + var client = new WebPubSubClient(new WebPubSubClientCredential(async token => + { + return await serviceClient.GetClientAccessUriAsync(userId: "user1", roles: new string[] { "webpubsub.joinLeaveGroup.demogroup", "webpubsub.sendToGroup.demogroup" }); + })); + + client.Connected += e => + { + Console.WriteLine($"Connected: {e.ConnectionId}"); + return Task.CompletedTask; + }; + + await client.StartAsync(); + + var streaming = Console.ReadLine(); + while (streaming != null) + { + await client.SendToGroupAsync("demogroup", BinaryData.FromString(streaming), WebPubSubDataType.Text); + streaming = Console.ReadLine(); + } + } + } +} diff --git a/samples/csharp/clientpubsub-client-sdk/clientpub/clientpub.csproj b/samples/csharp/clientpubsub-client-sdk/clientpub/clientpub.csproj new file mode 100644 index 000000000..2d7094121 --- /dev/null +++ b/samples/csharp/clientpubsub-client-sdk/clientpub/clientpub.csproj @@ -0,0 +1,13 @@ + + + + Exe + net6.0 + + + + + + + + diff --git a/samples/csharp/clientpubsub-client-sdk/clientsub/Program.cs b/samples/csharp/clientpubsub-client-sdk/clientsub/Program.cs new file mode 100644 index 000000000..f742ffcb0 --- /dev/null +++ b/samples/csharp/clientpubsub-client-sdk/clientsub/Program.cs @@ -0,0 +1,46 @@ +using System; +using System.Threading.Tasks; + +using Azure.Messaging.WebPubSub; +using Azure.Messaging.WebPubSub.Clients; + +namespace clientsub +{ + class Program + { + static async Task Main(string[] args) + { + if (args.Length != 2) + { + Console.WriteLine("Usage: clientsub "); + return; + } + var connectionString = args[0]; + var hub = args[1]; + + // Either generate the URL or fetch it from server or fetch a temp one from the portal + var serviceClient = new WebPubSubServiceClient(connectionString, hub); + + var client = new WebPubSubClient(new WebPubSubClientCredential(async token => + { + return await serviceClient.GetClientAccessUriAsync(userId: "user1", roles: new string[] { "webpubsub.joinLeaveGroup.demogroup", "webpubsub.sendToGroup.demogroup" }); + })); + + client.Connected += e => + { + Console.WriteLine($"Connected: {e.ConnectionId}"); + return Task.CompletedTask; + }; + + client.GroupMessageReceived += e => + { + Console.WriteLine($"Message received: {e.Message.Data}"); + return Task.CompletedTask; + }; + + await client.StartAsync(); + await client.JoinGroupAsync("demogroup"); + Console.Read(); + } + } +} diff --git a/samples/csharp/clientpubsub-client-sdk/clientsub/clientsub.csproj b/samples/csharp/clientpubsub-client-sdk/clientsub/clientsub.csproj new file mode 100644 index 000000000..4f0864d3a --- /dev/null +++ b/samples/csharp/clientpubsub-client-sdk/clientsub/clientsub.csproj @@ -0,0 +1,13 @@ + + + + Exe + net6.0 + + + + + + + + From a114b402ca5de9e98bf3efd12d1c5a1f17a00e78 Mon Sep 17 00:00:00 2001 From: Chenyang Liu Date: Sun, 29 Jan 2023 15:11:55 +0800 Subject: [PATCH 2/2] Update readme --- samples/csharp/clientpubsub-client-sdk/Readme.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/samples/csharp/clientpubsub-client-sdk/Readme.md b/samples/csharp/clientpubsub-client-sdk/Readme.md index 34e7ffaa4..e08f56231 100644 --- a/samples/csharp/clientpubsub-client-sdk/Readme.md +++ b/samples/csharp/clientpubsub-client-sdk/Readme.md @@ -5,6 +5,10 @@ 1. [NET 6 or above](https://docs.microsoft.com/dotnet) 2. Create an Azure Web PubSub resource +## Overview + +With client sdk `Azure.Messaging.WebPubSub.Client`, it's easy to communicate among clients and leverage reliable protocol by default to overcome transient connection drop. + ## Setup Copy **Connection String** from **Keys** tab of the created Azure Web PubSub service, and replace the `` below with the value of your **Connection String**. @@ -12,12 +16,14 @@ Copy **Connection String** from **Keys** tab of the created Azure Web PubSub ser ![Connection String](./../../../docs/images/portal_conn.png) 1. Start Client subscriber + ```bash cd clientsub dotnet run -- "" pubsubhub ``` 2. Start Client publisher + ```bash cd clientpub dotnet run -- "ConnectionString>" pubsubhub