ASPNET Core Server Sent Events / Response flush(ASPNET Core 服务器发送事件/响应刷新)
问题描述
虽然没有官方文档,但有谁知道如何使用 ASP.NET Core 实现 SSE?
While there is no official documentation, does anyone know how SSE may be implemented using ASP.NET Core?
我怀疑一种实现可能会使用自定义中间件,但也许可以在控制器操作中做到这一点?
I suspect one implementation may use custom middleware, but maybe it is possible to do that in controller action?
推荐答案
客户端 - wwwroot/index.html
在页面加载时,为 http://www.somehost.ca/sse 网址创建一个 EventSource.然后将其事件写入控制台.
Client Side - wwwroot/index.html
On page load, create an EventSource for the http://www.somehost.ca/sse url. Then write its events to the console.
<body>
<script type="text/javascript">
var source = new EventSource('sse');
source.onmessage = function (event) {
console.log('onmessage: ' + event.data);
};
source.onopen = function(event) {
console.log('onopen');
};
source.onerror = function(event) {
console.log('onerror');
}
</script>
</body>
服务器端替代方案 #1 - 使用中间件
中间件处理 sse 路径.它将 Content-Type 标头设置为服务器套接字事件所需的 text/event-stream.它写入响应流,而不关闭连接.它通过在写入之间延迟五秒钟来模仿工作.
Server Side Alternative #1 - Use Middleware
The middleware handles the sse path. It sets the Content-Type header to text/event-stream, which the server socket event requires. It writes to the response stream, without closing the connection. It mimics doing work, by delaying for five seconds between writes.
app.Use(async (context, next) =>
{
if (context.Request.Path.ToString().Equals("/sse"))
{
var response = context.Response;
response.Headers.Add("Content-Type", "text/event-stream");
for(var i = 0; true; ++i)
{
// WriteAsync requires `using Microsoft.AspNetCore.Http`
await response
.WriteAsync($"data: Middleware {i} at {DateTime.Now}
");
await response.Body.FlushAsync();
await Task.Delay(5 * 1000);
}
}
await next.Invoke();
});
服务器端替代方案 #2 - 使用控制器
控制器的作用与中间件完全相同.
Server Side Alternative #2 - Use a Controller
The controller does the exact same thing as the middleware does.
[Route("/api/sse")]
public class ServerSentEventController : Controller
{
[HttpGet]
public async Task Get()
{
var response = Response;
response.Headers.Add("Content-Type", "text/event-stream");
for(var i = 0; true; ++i)
{
await response
.WriteAsync($"data: Controller {i} at {DateTime.Now}
");
response.Body.Flush();
await Task.Delay(5 * 1000);
}
}
}
Firefox 中的客户端控制台输出
这是 Firefox 控制台窗口中的结果.每五秒钟就有一条新消息到达.
Client Side Console Output in Firefox
This is the result in the Firefox console window. Every five seconds a new messages arrives.
onopen
onmessage: Message 0 at 4/15/2016 3:39:04 PM
onmessage: Message 1 at 4/15/2016 3:39:09 PM
onmessage: Message 2 at 4/15/2016 3:39:14 PM
onmessage: Message 3 at 4/15/2016 3:39:19 PM
onmessage: Message 4 at 4/15/2016 3:39:24 PM
参考文献:
- GitHub 上的上述示例
- HTML 生活标准,第 9.2 节服务器发送的事件
- 维基百科上的Http推送技术
- 分块传输编码
这篇关于ASPNET Core 服务器发送事件/响应刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASPNET Core 服务器发送事件/响应刷新
基础教程推荐
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 如果条件可以为空 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
