Skip to content

Commit ae97070

Browse files
Merge pull request #250869 from vicancy/patch-8
Update socketio-build-realtime-code-streaming-app.md
2 parents 55c0b05 + 8a324c5 commit ae97070

File tree

2 files changed

+43
-79
lines changed

2 files changed

+43
-79
lines changed

articles/azure-web-pubsub/socketio-build-realtime-code-streaming-app.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ You can clone the repo and run `npm install` to install project dependencies.
398398
### Start the server
399399
400400
```bash
401-
node index.js <web-pubsub-connection-string>
401+
node server.js <web-pubsub-connection-string>
402402
```
403403
404404
This is the connection string that you received in [an earlier step](#get-a-connection-string).

articles/azure-web-pubsub/tutorial-subprotocol.md

Lines changed: 42 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -149,68 +149,34 @@ Now let's create a web application using the `json.webpubsub.azure.v1` subprotoc
149149

150150
# [C#](#tab/csharp)
151151

152-
Update `Startup.cs` with the below code.
153-
- Update the `ConfigureServices` method to add the service client, and read the connection string from configuration.
154-
- Update the `Configure` method to add `app.UseStaticFiles();` before `app.UseRouting();` to support static files.
155-
- And update `app.UseEndpoints` to generate the client access token with `/negotiate` requests.
152+
Update `Program.cs` with the below code.
153+
- Use `AddAzureClients` to add the service client, and read the connection string from configuration.
154+
- Add `app.UseStaticFiles();` before `app.Run();` to support static files.
155+
- And update `app.MapGet` to generate the client access token with `/negotiate` requests.
156156
157157
```csharp
158158
using Azure.Messaging.WebPubSub;
159-
160-
using Microsoft.AspNetCore.Builder;
161-
using Microsoft.AspNetCore.Hosting;
162-
using Microsoft.AspNetCore.Http;
163159
using Microsoft.Extensions.Azure;
164-
using Microsoft.Extensions.Configuration;
165-
using Microsoft.Extensions.DependencyInjection;
166-
using Microsoft.Extensions.Hosting;
167160

168-
namespace logstream
161+
var builder = WebApplication.CreateBuilder(args);
162+
builder.Services.AddAzureClients(s =>
169163
{
170-
public class Startup
171-
{
172-
public Startup(IConfiguration configuration)
173-
{
174-
Configuration = configuration;
175-
}
176-
177-
public IConfiguration Configuration { get; }
178-
179-
public void ConfigureServices(IServiceCollection services)
180-
{
181-
services.AddAzureClients(builder =>
182-
{
183-
builder.AddWebPubSubServiceClient(Configuration["Azure:WebPubSub:ConnectionString"], "stream");
184-
});
185-
}
186-
187-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
188-
{
189-
if (env.IsDevelopment())
190-
{
191-
app.UseDeveloperExceptionPage();
192-
}
193-
194-
app.UseStaticFiles();
195-
196-
app.UseRouting();
164+
s.AddWebPubSubServiceClient(builder.Configuration["Azure:WebPubSub:ConnectionString"], "stream");
165+
});
197166

198-
app.UseEndpoints(endpoints =>
199-
{
200-
endpoints.MapGet("/negotiate", async context =>
201-
{
202-
var service = context.RequestServices.GetRequiredService<WebPubSubServiceClient>();
203-
var response = new
204-
{
205-
url = service.GetClientAccessUri(roles: new string[] { "webpubsub.sendToGroup.stream", "webpubsub.joinLeaveGroup.stream" }).AbsoluteUri
206-
};
207-
await context.Response.WriteAsJsonAsync(response);
208-
});
209-
});
210-
}
211-
}
212-
}
167+
var app = builder.Build();
168+
app.UseStaticFiles();
169+
app.MapGet("/negotiate", async context =>
170+
{
171+
var service = context.RequestServices.GetRequiredService<WebPubSubServiceClient>();
172+
var response = new
173+
{
174+
url = service.GetClientAccessUri(roles: new string[] { "webpubsub.sendToGroup.stream", "webpubsub.joinLeaveGroup.stream" }).AbsoluteUri
175+
};
176+
await context.Response.WriteAsJsonAsync(response);
177+
});
213178

179+
app.Run();
214180
```
215181
216182
# [JavaScript](#tab/javascript)
@@ -366,32 +332,30 @@ Now let's create a web application using the `json.webpubsub.azure.v1` subprotoc
366332
367333
```html
368334
<html>
369-
370-
<body>
371-
<div id="output"></div>
372-
<script>
373-
(async function () {
374-
let res = await fetch('/negotiate')
375-
let data = await res.json();
376-
let ws = new WebSocket(data.url, 'json.webpubsub.azure.v1');
377-
ws.onopen = () => {
378-
console.log('connected');
379-
};
380-
381-
let output = document.querySelector('#output');
382-
ws.onmessage = event => {
383-
let d = document.createElement('p');
384-
d.innerText = event.data;
385-
output.appendChild(d);
386-
};
387-
})();
388-
</script>
389-
</body>
390-
391-
</html>
335+
<body>
336+
<div id="output"></div>
337+
<script>
338+
(async function () {
339+
let res = await fetch('/negotiate')
340+
let data = await res.json();
341+
let ws = new WebSocket(data.url, 'json.webpubsub.azure.v1');
342+
ws.onopen = () => {
343+
console.log('connected');
344+
};
345+
346+
let output = document.querySelector('#output');
347+
ws.onmessage = event => {
348+
let d = document.createElement('p');
349+
d.innerText = event.data;
350+
output.appendChild(d);
351+
};
352+
})();
353+
</script>
354+
</body>
355+
</html>
392356
```
393357
394-
It just connects to the service and print any message received to the page. The main change is that we specify the subprotocol when creating the WebSocket connection.
358+
The code above connects to the service and print any message received to the page. The main change is that we specify the subprotocol when creating the WebSocket connection.
395359
396360
4. Run the server
397361
# [C#](#tab/csharp)

0 commit comments

Comments
 (0)