Skip to content

Commit 16f7180

Browse files
committed
resolve comments
1 parent 9703b71 commit 16f7180

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ You can test the server by running `dotnet run --urls http://localhost:8080` and
114114
115115
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.
116116
117-
1. Install dependencies and use [Secret Manager](/aspnet/core/security/app-secrets#secret-manager) tool for .NET Core to set the connection string. Run the below command, replacing `<connection_string>` with the one fetched in [previous step](#get-the-connectionstring-for-future-use).
117+
1. Install dependencies.
118118
119119
```bash
120120
dotnet add package Microsoft.Extensions.Azure
121121
```
122122
123-
2. Add a `SampleChatHub.cs` class to handle hub events.
123+
2. Add a `SampleChatHub` class to handle hub events. And DI the service middleware and service client inside `ConfigureServices`. Don't forget to replace `<connection_string>` with the one of your services.
124124
125125
```csharp
126-
public class SampleChatHub : WebPubSubHub
126+
private sealed class SampleChatHub : WebPubSubHub
127127
{
128128
private readonly WebPubSubServiceClient<SampleChatHub> _serviceClient;
129129

@@ -132,23 +132,17 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
132132
_serviceClient = serviceClient;
133133
}
134134
}
135-
```
136-
137-
Here we use the `WebPubSubServiceClient<SampleChatHub>` to invoke rest API calls to service. This is bind with the DI method `AddWebPubSubServiceClient<SampleChatHub>()` in the next step.
138-
139-
3. DI the service middleware and service client inside `ConfigureServices` and don't forget to replace `<connection_string>` with the one of your services.
140-
141-
```csharp
135+
142136
public void ConfigureServices(IServiceCollection services)
143137
{
144138
services.AddWebPubSub(o => o.ServiceEndpoint = new ServiceEndpoint("<connection_string>"))
145139
.AddWebPubSubServiceClient<SampleChatHub>();
146140
}
147141
```
148142
149-
`AddWebPubSubServiceClient<THub>` is used to inject the service client, with which you can generate client connection token and invoke service to do something when hub events are triggered.
143+
`AddWebPubSubServiceClient<THub>` is used to inject the service client, with which we can generate client connection token and invoke service REST APIs when hub events are triggered.
150144
151-
4. Add a `/negotiate` API to the server inside `app.UseEndpoints` to generate the token.
145+
3. Add a `/negotiate` API to the server inside `app.UseEndpoints` to generate the token.
152146
153147
```csharp
154148
app.UseEndpoints(endpoints =>
@@ -172,7 +166,7 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
172166
173167
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.
174168
175-
5. Then update `index.html` to include the following script to get the token from server and connect to service.
169+
4. Then update `index.html` to include the following script to get the token from server and connect to service.
176170
177171
```html
178172
<html>
@@ -497,9 +491,9 @@ Here we're using Web PubSub middleware SDK, there is already an implementation t
497491
});
498492
```
499493
500-
2. Go the `SampleChatHub.cs` file we created in previous step and add logic we'd like server to invoke service when subscribed events are triggered.
494+
2. Go the `SampleChatHub` we created in previous step and override `OnConnectedAsync()` method we'd like server to invoke service when `connected` event is triggered.
501495
```csharp
502-
public class SampleChatHub : WebPubSubHub
496+
private sealed class SampleChatHub : WebPubSubHub
503497
{
504498
private readonly WebPubSubServiceClient<SampleChatHub> _serviceClient;
505499

@@ -616,12 +610,12 @@ Besides system events like `connected` or `disconnected`, client can also send m
616610
617611
# [C#](#tab/csharp)
618612
619-
Implement the `OnMessageReceivedAsync()` method in `SampleChatHub.cs`.
613+
Implement the `OnMessageReceivedAsync()` method in `SampleChatHub`.
620614
621615
1. Handle message event.
622616
623617
```csharp
624-
public class SampleChatHub : WebPubSubHub
618+
private sealed class SampleChatHub : WebPubSubHub
625619
{
626620
private readonly WebPubSubServiceClient<SampleChatHub> _serviceClient;
627621
@@ -644,7 +638,7 @@ Implement the `OnMessageReceivedAsync()` method in `SampleChatHub.cs`.
644638
}
645639
```
646640
647-
This event handler uses `WebPubSubServiceClient.SendToAllAsync()` to broadcast the received message to all clients. You can see in the end we returned `UserEventResponse`, which contains a message directly to the caller and make the WebHook request success. If you have extra logic to validate and would like to break this call, you can directly throw an exception here. The middleware will deliver the exception message to service and service will drop current connection.
641+
This event handler uses `WebPubSubServiceClient.SendToAllAsync()` to broadcast the received message to all clients. You can see in the end we returned `UserEventResponse`, which contains a message directly to the caller and make the WebHook request success. If you have extra logic to validate and would like to break this call, you can throw an exception here. The middleware will deliver the exception message to service and service will drop current client connection.
648642
649643
2. Update `index.html` to add the logic to send message from user to server and display received messages in the page.
650644

0 commit comments

Comments
 (0)