Skip to content

Commit d5e0a2a

Browse files
authored
Merge pull request #180053 from vicancy/lianwei/sdk2
Update to use latest SDK
2 parents 220fc88 + 685c4bb commit d5e0a2a

File tree

4 files changed

+321
-63
lines changed

4 files changed

+321
-63
lines changed

articles/azure-web-pubsub/quickstart-use-sdk.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
6969
mkdir publisher
7070
cd publisher
7171
dotnet new console
72-
dotnet add package Azure.Messaging.WebPubSub --version 1.0.0-beta.3
72+
dotnet add package Azure.Messaging.WebPubSub
7373
```
7474

7575
2. Update the `Program.cs` file to use the `WebPubSubServiceClient` class and send messages to the clients.
@@ -125,7 +125,7 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
125125
mkdir publisher
126126
cd publisher
127127
npm init -y
128-
npm install --save @azure/web-pubsub@1.0.0-alpha.20211102.4
128+
npm install --save @azure/web-pubsub
129129
```
130130
131131
2. Now let's use Azure Web PubSub SDK to publish a message to the service. Create a `publish.js` file with the below code:
@@ -221,20 +221,20 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
221221
<dependency>
222222
<groupId>com.azure</groupId>
223223
<artifactId>azure-messaging-webpubsub</artifactId>
224-
<version>1.0.0-beta.2</version>
224+
<version>1.0.0-beta.6</version>
225225
</dependency>
226226
```
227227
228228
3. Now let's use Azure Web PubSub SDK to publish a message to the service. Let's navigate to the */src/main/java/com/webpubsub/quickstart* directory, open the *App.java* file in your editor and replace code with the below:
229229
230230
```java
231231
package com.webpubsub.quickstart;
232-
232+
233233
import com.azure.messaging.webpubsub.*;
234234
import com.azure.messaging.webpubsub.models.*;
235-
235+
236236
/**
237-
* Quickstart - Publish messages using Azure Web PubSub service SDK
237+
* Publish messages using Azure Web PubSub service SDK
238238
*
239239
*/
240240
public class App
@@ -245,8 +245,8 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
245245
System.out.println("Expecting 3 arguments: <connection-string> <hub-name> <message>");
246246
return;
247247
}
248-
249-
WebPubSubServiceClient service = new WebPubSubClientBuilder()
248+
249+
WebPubSubServiceClient service = new WebPubSubServiceClientBuilder()
250250
.connectionString(args[0])
251251
.hub(args[1])
252252
.buildClient();

articles/azure-web-pubsub/tutorial-build-chat.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ First let's create an empty ASP.NET Core app.
7676

7777
```bash
7878
dotnet new web
79-
dotnet add package Azure.Messaging.WebPubSub --version 1.0.0-beta.3
79+
dotnet add package Azure.Messaging.WebPubSub
8080
```
8181

8282
2. Then add `app.UseStaticFiles();` before `app.UseRouting();` in `Startup.cs` to support static files. Remove the default `endpoints.MapGet` inside `app.UseEndpoints`.
@@ -149,7 +149,7 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
149149
return;
150150
}
151151
var serviceClient = context.RequestServices.GetRequiredService<WebPubSubServiceClient>();
152-
await context.Response.WriteAsync(serviceClient.GenerateClientAccessUri(userId: id).AbsoluteUri);
152+
await context.Response.WriteAsync(serviceClient.GetClientAccessUri(userId: id).AbsoluteUri);
153153
});
154154
});
155155
```
@@ -224,7 +224,7 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
224224
1. Install Azure Web PubSub SDK
225225
226226
```bash
227-
npm install --save @azure/web-pubsub@1.0.0-alpha.20211102.4
227+
npm install --save @azure/web-pubsub
228228
```
229229
230230
2. Add a `/negotiate` API to the server to generate the token
@@ -378,7 +378,7 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
378378
<dependency>
379379
<groupId>com.azure</groupId>
380380
<artifactId>azure-messaging-webpubsub</artifactId>
381-
<version>1.0.0-beta.2</version>
381+
<version>1.0.0-beta.6</version>
382382
</dependency>
383383
```
384384
@@ -387,11 +387,11 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
387387
```java
388388
package com.webpubsub.tutorial;
389389
390-
import com.azure.messaging.webpubsub.WebPubSubClientBuilder;
391390
import com.azure.messaging.webpubsub.WebPubSubServiceClient;
392-
import com.azure.messaging.webpubsub.models.GetAuthenticationTokenOptions;
393-
import com.azure.messaging.webpubsub.models.WebPubSubAuthenticationToken;
394-
391+
import com.azure.messaging.webpubsub.WebPubSubServiceClientBuilder;
392+
import com.azure.messaging.webpubsub.models.GetClientAccessTokenOptions;
393+
import com.azure.messaging.webpubsub.models.WebPubSubClientAccessToken;
394+
import com.azure.messaging.webpubsub.models.WebPubSubContentType;
395395
import io.javalin.Javalin;
396396
397397
public class App {
@@ -403,7 +403,7 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
403403
}
404404
405405
// create the service client
406-
WebPubSubServiceClient client = new WebPubSubClientBuilder()
406+
WebPubSubServiceClient service = new WebPubSubServiceClientBuilder()
407407
.connectionString(args[0])
408408
.hub("chat")
409409
.buildClient();
@@ -422,9 +422,10 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
422422
ctx.result("missing user id");
423423
return;
424424
}
425-
GetAuthenticationTokenOptions option = new GetAuthenticationTokenOptions();
425+
GetClientAccessTokenOptions option = new GetClientAccessTokenOptions();
426426
option.setUserId(id);
427-
WebPubSubAuthenticationToken token = client.getAuthenticationToken(option);
427+
WebPubSubClientAccessToken token = service.getClientAccessToken(option);
428+
428429
ctx.result(token.getUrl());
429430
return;
430431
});
@@ -895,7 +896,7 @@ The `ce-type` of `message` event is always `azure.webpubsub.user.message`, detai
895896
} else if ("azure.webpubsub.user.message".equals(event)) {
896897
String id = ctx.header("ce-userId");
897898
String message = ctx.body();
898-
client.sendToAll(String.format("[%s] %s", id, message), WebPubSubContentType.TEXT_PLAIN);
899+
service.sendToAll(String.format("[%s] %s", id, message), WebPubSubContentType.TEXT_PLAIN);
899900
}
900901
ctx.status(200);
901902
});
@@ -951,11 +952,11 @@ The `ce-type` of `message` event is always `azure.webpubsub.user.message`, detai
951952
String event = ctx.header("ce-type");
952953
if ("azure.webpubsub.sys.connected".equals(event)) {
953954
String id = ctx.header("ce-userId");
954-
client.sendToAll(String.format("[SYSTEM] %s joined", id), WebPubSubContentType.TEXT_PLAIN);
955+
service.sendToAll(String.format("[SYSTEM] %s joined", id), WebPubSubContentType.TEXT_PLAIN);
955956
} else if ("azure.webpubsub.user.message".equals(event)) {
956957
String id = ctx.header("ce-userId");
957958
String message = ctx.body();
958-
client.sendToAll(String.format("[%s] %s", id, message), WebPubSubContentType.TEXT_PLAIN);
959+
service.sendToAll(String.format("[%s] %s", id, message), WebPubSubContentType.TEXT_PLAIN);
959960
}
960961
ctx.status(200);
961962
});

articles/azure-web-pubsub/tutorial-pub-sub-messages.md

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,19 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
7777
cd subscriber
7878
dotnet new console
7979
dotnet add package Websocket.Client --version 4.3.30
80-
dotnet add package Azure.Messaging.WebPubSub --version 1.0.0-beta.3
80+
dotnet add package Azure.Messaging.WebPubSub --version 1.0.0
8181
```
8282

8383
2. Update the `Program.cs` file to connect to the service:
8484

8585
```csharp
8686
using System;
8787
using System.Threading.Tasks;
88+
8889
using Azure.Messaging.WebPubSub;
90+
8991
using Websocket.Client;
90-
92+
9193
namespace subscriber
9294
{
9395
class Program
@@ -101,11 +103,11 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
101103
}
102104
var connectionString = args[0];
103105
var hub = args[1];
104-
106+
105107
// Either generate the URL or fetch it from server or fetch a temp one from the portal
106-
var service = new WebPubSubServiceClient(connectionString, hub);
107-
var url = service.GenerateClientAccessUri();
108-
108+
var serviceClient = new WebPubSubServiceClient(connectionString, hub);
109+
var url = serviceClient.GetClientAccessUri();
110+
109111
using (var client = new WebsocketClient(url))
110112
{
111113
// Disable the auto disconnect and reconnect because the sample would like the client to stay online even no data comes in
@@ -118,11 +120,12 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
118120
}
119121
}
120122
}
123+
121124
```
122125
123126
The code above creates a WebSocket connection to connect to a hub in Azure Web PubSub. Hub is a logical unit in Azure Web PubSub where you can publish messages to a group of clients. [Key concepts](./key-concepts.md) contains the detailed explanation about the terms used in Azure Web PubSub.
124127
125-
Azure Web PubSub service uses [JSON Web Token (JWT)](../active-directory/develop/security-tokens.md#json-web-tokens-and-claims) authentication, so in the code sample we use `WebPubSubServiceClient.GenerateClientAccessUri()` in Web PubSub SDK to generate a url to the service that contains the full URL with a valid access token.
128+
Azure Web PubSub service uses [JSON Web Token (JWT)](../active-directory/develop/security-tokens.md#json-web-tokens-and-claims) authentication, so in the code sample we use `WebPubSubServiceClient.GetClientAccessUri()` in Web PubSub SDK to generate a url to the service that contains the full URL with a valid access token.
126129
127130
After the connection is established, you'll receive messages through the WebSocket connection. So we use `client.MessageReceived.Subscribe(msg => ...));` to listen to incoming messages.
128131
@@ -141,7 +144,7 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
141144
cd subscriber
142145
npm init -y
143146
npm install --save ws
144-
npm install --save @azure/web-pubsub@1.0.0-alpha.20211102.4
147+
npm install --save @azure/web-pubsub
145148
146149
```
147150
2. Then use WebSocket API to connect to service. Create a `subscribe.js` file with the below code:
@@ -262,7 +265,7 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
262265
<dependency>
263266
<groupId>com.azure</groupId>
264267
<artifactId>azure-messaging-webpubsub</artifactId>
265-
<version>1.0.0-beta.2</version>
268+
<version>1.0.0-beta.6</version>
266269
</dependency>
267270
268271
<dependency>
@@ -276,19 +279,18 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
276279
3. In Azure Web PubSub, you can connect to the service and subscribe to messages through WebSocket connections. WebSocket is a full-duplex communication channel so service can push messages to your client in real time. You can use any API/library that supports WebSocket to do so. For this sample, we use package [Java-WebSocket](https://github.com/TooTallNate/Java-WebSocket). Let's navigate to the */src/main/java/com/webpubsub/quickstart* directory, open the *App.java* file in your editor, and replace code with the below:
277280
278281
```java
279-
280282
package com.webpubsub.quickstart;
281-
283+
282284
import com.azure.messaging.webpubsub.*;
283285
import com.azure.messaging.webpubsub.models.*;
284286
285287
import org.java_websocket.client.WebSocketClient;
286288
import org.java_websocket.handshake.ServerHandshake;
287-
289+
288290
import java.io.IOException;
289291
import java.net.URI;
290292
import java.net.URISyntaxException;
291-
293+
292294
/**
293295
* Connect to Azure Web PubSub service using WebSocket protocol
294296
*
@@ -301,37 +303,37 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
301303
System.out.println("Expecting 2 arguments: <connection-string> <hub-name>");
302304
return;
303305
}
304-
305-
WebPubSubServiceClient service = new WebPubSubClientBuilder()
306+
307+
WebPubSubServiceClient service = new WebPubSubServiceClientBuilder()
306308
.connectionString(args[0])
307309
.hub(args[1])
308310
.buildClient();
309-
310-
WebPubSubAuthenticationToken token = service.getAuthenticationToken(new GetAuthenticationTokenOptions());
311-
311+
312+
WebPubSubClientAccessToken token = service.getClientAccessToken(new GetClientAccessTokenOptions());
313+
312314
WebSocketClient webSocketClient = new WebSocketClient(new URI(token.getUrl())) {
313315
@Override
314316
public void onMessage(String message) {
315317
System.out.println(String.format("Message received: %s", message));
316318
}
317-
319+
318320
@Override
319321
public void onClose(int arg0, String arg1, boolean arg2) {
320322
// TODO Auto-generated method stub
321323
}
322-
324+
323325
@Override
324326
public void onError(Exception arg0) {
325327
// TODO Auto-generated method stub
326328
}
327-
329+
328330
@Override
329331
public void onOpen(ServerHandshake arg0) {
330332
// TODO Auto-generated method stub
331333
}
332-
334+
333335
};
334-
336+
335337
webSocketClient.connect();
336338
System.in.read();
337339
}
@@ -341,7 +343,7 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
341343
342344
The code above creates a WebSocket connection to connect to a hub in Azure Web PubSub. Hub is a logical unit in Azure Web PubSub where you can publish messages to a group of clients. [Key concepts](./key-concepts.md) contains the detailed explanation about the terms used in Azure Web PubSub.
343345
344-
Azure Web PubSub service uses [JSON Web Token (JWT)](../active-directory/develop/security-tokens.md#json-web-tokens-and-claims) authentication, so in the code sample we use `WebPubSubServiceClient.getAuthenticationToken(new GetAuthenticationTokenOptions())` in Web PubSub SDK to generate a url to the service that contains the full URL with a valid access token.
346+
Azure Web PubSub service uses [JSON Web Token (JWT)](../active-directory/develop/security-tokens.md#json-web-tokens-and-claims) authentication, so in the code sample we use `WebPubSubServiceClient.getClientAccessToken(new GetClientAccessTokenOptions())` in Web PubSub SDK to generate a url to the service that contains the full URL with a valid access token.
345347
346348
After connection is established, you'll receive messages through the WebSocket connection. So we use `onMessage(String message)` to listen to incoming messages.
347349
@@ -365,7 +367,7 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
365367
mkdir publisher
366368
cd publisher
367369
dotnet new console
368-
dotnet add package Azure.Messaging.WebPubSub --version 1.0.0-beta.3
370+
dotnet add package Azure.Messaging.WebPubSub
369371
```
370372
371373
2. Now let's update the `Program.cs` file to use the `WebPubSubServiceClient` class and send messages to the clients.
@@ -374,7 +376,7 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
374376
using System;
375377
using System.Threading.Tasks;
376378
using Azure.Messaging.WebPubSub;
377-
379+
378380
namespace publisher
379381
{
380382
class Program
@@ -388,15 +390,14 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
388390
var connectionString = args[0];
389391
var hub = args[1];
390392
var message = args[2];
391-
392-
var service = new WebPubSubServiceClient(connectionString, hub);
393393
394-
// Send messages to all the connected clients
395-
// You can also try SendToConnectionAsync to send messages to the specific connection
396-
await service.SendToAllAsync(message);
394+
// Either generate the token or fetch it from server or fetch a temp one from the portal
395+
var serviceClient = new WebPubSubServiceClient(connectionString, hub);
396+
await serviceClient.SendToAllAsync(message);
397397
}
398398
}
399399
}
400+
400401
```
401402
402403
The `SendToAllAsync()` call simply sends a message to all connected clients in the hub.
@@ -421,7 +422,7 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
421422
mkdir publisher
422423
cd publisher
423424
npm init -y
424-
npm install --save @azure/web-pubsub@1.0.0-alpha.20211102.4
425+
npm install --save @azure/web-pubsub
425426
426427
```
427428
2. Now let's use Azure Web PubSub SDK to publish a message to the service. Create a `publish.js` file with the below code:
@@ -514,18 +515,19 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
514515
<dependency>
515516
<groupId>com.azure</groupId>
516517
<artifactId>azure-messaging-webpubsub</artifactId>
517-
<version>1.0.0-beta.2</version>
518+
<version>1.0.0-beta.6</version>
518519
</dependency>
519520
```
520521
521522
3. Now let's use Azure Web PubSub SDK to publish a message to the service. Let's navigate to the */src/main/java/com/webpubsub/quickstart* directory, open the *App.java* file in your editor, and replace code with the below:
522523
523524
```java
524-
package com.webpubsub.quickstart;
525525
526+
package com.webpubsub.quickstart;
527+
526528
import com.azure.messaging.webpubsub.*;
527529
import com.azure.messaging.webpubsub.models.*;
528-
530+
529531
/**
530532
* Publish messages using Azure Web PubSub service SDK
531533
*
@@ -538,8 +540,8 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
538540
System.out.println("Expecting 3 arguments: <connection-string> <hub-name> <message>");
539541
return;
540542
}
541-
542-
WebPubSubServiceClient service = new WebPubSubClientBuilder()
543+
544+
WebPubSubServiceClient service = new WebPubSubServiceClientBuilder()
543545
.connectionString(args[0])
544546
.hub(args[1])
545547
.buildClient();

0 commit comments

Comments
 (0)