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
@@ -151,7 +151,7 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
151
151
}
152
152
```
153
153
154
-
`AddWebPubSubServiceClient<THub>()` is used to inject the service client `WebPubSubServiceClient<THub>`, with which we can use in negotiation step to generate client connection token and in hub methods to invoke service REST APIs when hub events are triggered.
154
+
`AddWebPubSubServiceClient<THub>()` is used to inject the service client `WebPubSubServiceClient<THub>`, with which we can use in negotiation step to generate client connection token and in hub methods to invoke service REST APIs when hub events are triggered.
155
155
156
156
3. Add a `/negotiate` API to the server inside `app.UseEndpoints` to generate the token.
157
157
@@ -173,9 +173,9 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
173
173
});
174
174
```
175
175
176
-
This token generation code is similar to the one we used in the [publish and subscribe message tutorial](./tutorial-pub-sub-messages.md), except we pass one more argument (`userId`) when generating the token. User ID can be used to identify the identity of client so when you receive a message you know where the message is coming from.
176
+
This token generation code is similar to the one we used in the [publish and subscribe message tutorial](./tutorial-pub-sub-messages.md), except we pass one more argument (`userId`) when generating the token. User ID can be used to identify the identity of client so when you receive a message you know where the message is coming from.
177
177
178
-
You can test this API by running `dotnet run --urls http://localhost:8080` and accessing `http://localhost:8080/negotiate?id=<user-id>` and it will give you the full url of the Azure Web PubSub with an access token.
178
+
You can test this API by running `dotnet run --urls http://localhost:8080` and accessing `http://localhost:8080/negotiate?id=<user-id>` and it will give you the full url of the Azure Web PubSub with an access token.
179
179
180
180
4. Then update `index.html` to include the following script to get the token from server and connect to service.
181
181
@@ -197,7 +197,7 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
197
197
</html>
198
198
```
199
199
200
-
If you are using Chrome, you can test it by opening the home page, input your user name. Press F12 to open the Developer Tools window, switch to **Console** table and you'll see `connected` being printed in browser console.
200
+
If you are using Chrome, you can test it by opening the home page, input your user name. Press F12 to open the Developer Tools window, switch to **Console** table and you'll see `connected` being printed in browser console.
201
201
202
202
# [C# .NET 6](#tab/net6)
203
203
We'll use [ASP.NET Core 6](/aspnet/core) to host the web pages and handle incoming requests.
@@ -236,6 +236,7 @@ First let's create an empty ASP.NET Core app.
236
236
</body>
237
237
</html>
238
238
```
239
+
239
240
You can test the server by running `dotnet run --urls http://localhost:8080` and access http://localhost:8080/index.html in browser.
240
241
241
242
You may remember in the [publish and subscribe message tutorial](./tutorial-pub-sub-messages.md) the subscriber uses an API in Web PubSub SDK to generate an access token from connection string and use it to connect to the service. This is usually not safe in a real world application as connection string has high privilege to do any operation to the service so you don't want to share it with any client. Let's change this access token generation process to a REST API at server side, so client can call this API to request an access token every time it needs to connect, without need to hold the connection string.
@@ -249,43 +250,43 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
249
250
2. Create a file `SampleChatHub.cs`for the `SampleChatHub` class to handle hub events with the following content.
250
251
251
252
```csharp
252
-
using Microsoft.Azure.WebPubSub.AspNetCore;
253
+
using Microsoft.Azure.WebPubSub.AspNetCore;
253
254
254
-
sealed class SampleChatHub : WebPubSubHub
255
-
{
256
-
}
255
+
sealed class SampleChatHub : WebPubSubHub
256
+
{
257
+
}
257
258
```
258
259
259
260
3. Then update the `Program.cs` file to add DI for the service middleware and service client inside. Don't forget to replace `<connection_string>` with the one of your services.
260
261
261
262
```csharp
262
-
using Microsoft.Azure.WebPubSub.AspNetCore;
263
+
using Microsoft.Azure.WebPubSub.AspNetCore;
263
264
264
-
var builder = WebApplication.CreateBuilder(args);
265
+
var builder = WebApplication.CreateBuilder(args);
265
266
266
-
builder.Services.AddWebPubSub(
267
-
o => o.ServiceEndpoint = new ServiceEndpoint("<connection_string>"))
268
-
.AddWebPubSubServiceClient<SampleChatHub>();
267
+
builder.Services.AddWebPubSub(
268
+
o => o.ServiceEndpoint = new ServiceEndpoint("<connection_string>"))
269
+
.AddWebPubSubServiceClient<SampleChatHub>();
269
270
270
-
var app = builder.Build();
271
+
var app = builder.Build();
271
272
272
-
if (app.Environment.IsDevelopment())
273
-
{
274
-
app.UseDeveloperExceptionPage();
275
-
}
273
+
if (app.Environment.IsDevelopment())
274
+
{
275
+
app.UseDeveloperExceptionPage();
276
+
}
276
277
277
-
app.UseDefaultFiles();
278
-
app.UseStaticFiles();
279
-
app.UseRouting();
278
+
app.UseDefaultFiles();
279
+
app.UseStaticFiles();
280
+
app.UseRouting();
280
281
281
-
app.UseEndpoints(endpoints =>
282
-
{
283
-
});
282
+
app.UseEndpoints(endpoints =>
283
+
{
284
+
});
284
285
285
-
app.Run();
286
+
app.Run();
286
287
```
287
288
288
-
`AddWebPubSubServiceClient<THub>()` is used to inject the service client `WebPubSubServiceClient<THub>`, with which we can use in negotiation step to generate client connection token and in hub methods to invoke service REST APIs when hub events are triggered.
289
+
`AddWebPubSubServiceClient<THub>()` is used to inject the service client `WebPubSubServiceClient<THub>`, with which we can use in negotiation step to generate client connection token and in hub methods to invoke service REST APIs when hub events are triggered.
289
290
290
291
4. Add a `/negotiate` API to the server inside `app.UseEndpoints` to generate the token.
291
292
@@ -305,9 +306,10 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
305
306
});
306
307
});
307
308
```
308
-
This token generation code is similar to the one we used in the [publish and subscribe message tutorial](./tutorial-pub-sub-messages.md), except we pass one more argument (`userId`) when generating the token. User ID can be used to identify the identity of client so when you receive a message you know where the message is coming from.
309
309
310
-
You can test this API by running `dotnet run --urls http://localhost:8080` and accessing `http://localhost:8080/negotiate?id=<user-id>` and it will give you the full url of the Azure Web PubSub with an access token.
310
+
This token generation code is similar to the one we used in the [publish and subscribe message tutorial](./tutorial-pub-sub-messages.md), except we pass one more argument (`userId`) when generating the token. User ID can be used to identify the identity of client so when you receive a message you know where the message is coming from.
311
+
312
+
You can test this API by running `dotnet run --urls http://localhost:8080` and accessing `http://localhost:8080/negotiate?id=<user-id>` and it will give you the full url of the Azure Web PubSub with an access token.
311
313
312
314
5. Then update `index.html` to include the following script to get the token from server and connect to service.
313
315
@@ -678,6 +680,7 @@ Here we're using Web PubSub middleware SDK, there is already an implementation t
678
680
```
679
681
680
682
2. Go the `SampleChatHub` we created in previous step. Add a constructor to work with `WebPubSubServiceClient<SampleChatHub>` so we can use to invoke service. And override `OnConnectedAsync()` method to respond when `connected` event is triggered.
0 commit comments