-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (94 loc) · 4.11 KB
/
Program.cs
File metadata and controls
120 lines (94 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System.Text.Json;
namespace AspNetCoreWebApiWithSSE
{
public class Program
{
public async static Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthorization();
// Add a permissive CORS policy for the demo (allow anything)
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
// Enable the demo CORS policy
app.UseCors("AllowAll");
app.UseAuthorization();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", (HttpContext httpContext) =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.MapGet("/weatherforecastevents", async (HttpContext context) =>
{
context.Response.Headers.ContentType = "text/event-stream";
context.Response.Headers.CacheControl = "no-cache";
context.Response.Headers.Connection = "keep-alive";
var forecast = Enumerable.Range(1, 1000).Select(index =>
new WeatherForecast
{
Id = Guid.NewGuid(),
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
for (var i = 0; i < 1000; i++)
{
var json = JsonSerializer.Serialize(forecast[i]);
// Use the forecast Id as the SSE event id so clients can track messages
await context.Response.WriteAsync($"id: {forecast[i].Id}\n");
// Use the forecast summary as the SSE event type so clients can filter by event type if they want
await context.Response.WriteAsync($"event: weather\n");
// Finally write the forecast data as JSON in the SSE data field...
await context.Response.WriteAsync($"data: {json}\n\n");
await context.Response.Body.FlushAsync();
await Task.Delay(Random.Shared.Next(100, 1500));
}
})
.WithName("GetWeatherForecastEvents");
app.MapGet("/api/events", async context =>
{
context.Response.Headers.ContentType = "text/event-stream";
for (var i = 1; i <= 10; i++)
{
await context.Response.WriteAsync(
$"data: {{\"text\":\"Message {i}\"}}\n\n"
);
await context.Response.Body.FlushAsync();
await Task.Delay(1000);
}
})
.WithName("events");
await app.RunAsync();
}
}
}