diff --git a/samples/csharp/clientpubsub-client-sdk/Readme.md b/samples/csharp/clientpubsub-client-sdk/Readme.md new file mode 100644 index 000000000..e08f56231 --- /dev/null +++ b/samples/csharp/clientpubsub-client-sdk/Readme.md @@ -0,0 +1,32 @@ +# 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 + +## 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**. + +![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 + + + + + + + +