You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
229
229
230
230
```java
231
231
package com.webpubsub.quickstart;
232
-
232
+
233
233
import com.azure.messaging.webpubsub.*;
234
234
import com.azure.messaging.webpubsub.models.*;
235
-
235
+
236
236
/**
237
-
*Quickstart - Publish messages using Azure Web PubSub service SDK
237
+
* Publish messages using Azure Web PubSub service SDK
238
238
*
239
239
*/
240
240
public class App
@@ -245,8 +245,8 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
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-
149
149
return;
150
150
}
151
151
var serviceClient = context.RequestServices.GetRequiredService<WebPubSubServiceClient>();
2. Update the `Program.cs` file to connect to the service:
84
84
85
85
```csharp
86
86
using System;
87
87
using System.Threading.Tasks;
88
+
88
89
using Azure.Messaging.WebPubSub;
90
+
89
91
using Websocket.Client;
90
-
92
+
91
93
namespace subscriber
92
94
{
93
95
class Program
@@ -101,11 +103,11 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
101
103
}
102
104
var connectionString = args[0];
103
105
var hub = args[1];
104
-
106
+
105
107
// 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
+
109
111
using (var client = new WebsocketClient(url))
110
112
{
111
113
// 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
118
120
}
119
121
}
120
122
}
123
+
121
124
```
122
125
123
126
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.
124
127
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.
126
129
127
130
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.
128
131
@@ -141,7 +144,7 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
@@ -276,19 +279,18 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
276
279
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:
public void onClose(int arg0, String arg1, boolean arg2) {
320
322
// TODO Auto-generated method stub
321
323
}
322
-
324
+
323
325
@Override
324
326
public void onError(Exception arg0) {
325
327
// TODO Auto-generated method stub
326
328
}
327
-
329
+
328
330
@Override
329
331
public void onOpen(ServerHandshake arg0) {
330
332
// TODO Auto-generated method stub
331
333
}
332
-
334
+
333
335
};
334
-
336
+
335
337
webSocketClient.connect();
336
338
System.in.read();
337
339
}
@@ -341,7 +343,7 @@ Clients connect to the Azure Web PubSub service through the standard WebSocket p
341
343
342
344
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.
343
345
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.
345
347
346
348
After connection is established, you'll receive messages through the WebSocket connection. So we use `onMessage(String message)` to listen to incoming messages.
347
349
@@ -365,7 +367,7 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
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:
522
523
523
524
```java
524
-
package com.webpubsub.quickstart;
525
525
526
+
package com.webpubsub.quickstart;
527
+
526
528
import com.azure.messaging.webpubsub.*;
527
529
import com.azure.messaging.webpubsub.models.*;
528
-
530
+
529
531
/**
530
532
* Publish messages using Azure Web PubSub service SDK
531
533
*
@@ -538,8 +540,8 @@ Now let's use Azure Web PubSub SDK to publish a message to the connected client.
0 commit comments