• <bdo id='7RPja'></bdo><ul id='7RPja'></ul>
        <tfoot id='7RPja'></tfoot>

        <small id='7RPja'></small><noframes id='7RPja'>

        <i id='7RPja'><tr id='7RPja'><dt id='7RPja'><q id='7RPja'><span id='7RPja'><b id='7RPja'><form id='7RPja'><ins id='7RPja'></ins><ul id='7RPja'></ul><sub id='7RPja'></sub></form><legend id='7RPja'></legend><bdo id='7RPja'><pre id='7RPja'><center id='7RPja'></center></pre></bdo></b><th id='7RPja'></th></span></q></dt></tr></i><div id='7RPja'><tfoot id='7RPja'></tfoot><dl id='7RPja'><fieldset id='7RPja'></fieldset></dl></div>
      1. <legend id='7RPja'><style id='7RPja'><dir id='7RPja'><q id='7RPja'></q></dir></style></legend>

        ASPNET Core 服务器发送事件/响应刷新

        时间:2023-07-11

          <small id='5rN0p'></small><noframes id='5rN0p'>

          <legend id='5rN0p'><style id='5rN0p'><dir id='5rN0p'><q id='5rN0p'></q></dir></style></legend>

        • <tfoot id='5rN0p'></tfoot>
          <i id='5rN0p'><tr id='5rN0p'><dt id='5rN0p'><q id='5rN0p'><span id='5rN0p'><b id='5rN0p'><form id='5rN0p'><ins id='5rN0p'></ins><ul id='5rN0p'></ul><sub id='5rN0p'></sub></form><legend id='5rN0p'></legend><bdo id='5rN0p'><pre id='5rN0p'><center id='5rN0p'></center></pre></bdo></b><th id='5rN0p'></th></span></q></dt></tr></i><div id='5rN0p'><tfoot id='5rN0p'></tfoot><dl id='5rN0p'><fieldset id='5rN0p'></fieldset></dl></div>
            <tbody id='5rN0p'></tbody>
              • <bdo id='5rN0p'></bdo><ul id='5rN0p'></ul>

                  本文介绍了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 服务器发送事件/响应刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:根据操作名称授权用户 下一篇:为什么 Razor Pages 是在 Asp.net Core 中创建 Web UI 的推荐方法?

                  相关文章

                  <i id='IrDPc'><tr id='IrDPc'><dt id='IrDPc'><q id='IrDPc'><span id='IrDPc'><b id='IrDPc'><form id='IrDPc'><ins id='IrDPc'></ins><ul id='IrDPc'></ul><sub id='IrDPc'></sub></form><legend id='IrDPc'></legend><bdo id='IrDPc'><pre id='IrDPc'><center id='IrDPc'></center></pre></bdo></b><th id='IrDPc'></th></span></q></dt></tr></i><div id='IrDPc'><tfoot id='IrDPc'></tfoot><dl id='IrDPc'><fieldset id='IrDPc'></fieldset></dl></div>

                      <bdo id='IrDPc'></bdo><ul id='IrDPc'></ul>
                    <tfoot id='IrDPc'></tfoot>

                      <legend id='IrDPc'><style id='IrDPc'><dir id='IrDPc'><q id='IrDPc'></q></dir></style></legend>
                    1. <small id='IrDPc'></small><noframes id='IrDPc'>