【问题标题】:How to config HTTPPlatformHandler of IIS for Server Sent Event (SSE, EventStream)如何为服务器发送事件(SSE、EventStream)配置 IIS 的 HTTPPlatformHandler
【发布时间】:2018-09-26 11:54:26
【问题描述】:

目前我有提供 SSE 即服务的程序,我必须在 IIS 上部署。但它不能正常工作, 这是我在没有 IIS 的情况下运行 .exe 时的结果。

data: Hello, world

但是当它在 IIS 后面运行时,浏览器在加载时卡住了。 我必须刷新事件Hello, world 千次才能使 IIS 刷新结果到浏览器,并且它会立即刷新,而不是像 SSE 那样进行增量更新。

这是我的web.config

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <handlers>
          <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform processPath=".\sse_server.exe" 
          arguments="-port=%HTTP_PLATFORM_PORT% -environment development" 
          stdoutLogEnabled="false" 
          requestTimeout="00:05:00" 
          stdoutLogFile=".\sse_server_log">
        </httpPlatform>
        <urlCompression doStaticCompression="true" doDynamicCompression="false" />
        <caching enabled="false" enableKernelCache="false" />
      </system.webServer>
    </configuration>

这是我的go 代码

func SSEHello(rw http.ResponseWriter, flusher http.Flusher) {
    rw.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
    rw.Header().Set("Cache-Control", "no-cache")
    rw.Header().Set("Connection", "keep-alive")
    rw.WriteHeader(http.StatusOK)
    for i := 0; i < 1000; i++ {
        rw.Write([]byte("data:Hello, world\n\n"))
        flusher.Flush()
        time.Sleep(time.Millisecond * 100)
    }
}

【问题讨论】:

    标签: go iis iis-express devops event-stream


    【解决方案1】:

    实际上 HttpPlatformHandler 有 8kb output buffer ,所以我的消息不会立即发送出去。

    我要换HttpPlatformHandler to ASP.NET Core Module, 所以web.config 必须更新到这个。

        <?xml version="1.0" encoding="utf-8"?>
        <configuration>
          <system.webServer>
            <handlers>
              <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath=".\sse_server.exe"  />
          </system.webServer>
        </configuration>
    

    并且要在iis 上以aspNetCore 启动go 的应用程序,应用程序需要获取环境变量名称ASPNETCORE_PORT 然后在该端口上启动http 服务。

    port := os.Getenv("ASPNETCORE_PORT")
    http.ListenAndServe(":"+port, nil)
    

    就是这样!

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2022-12-18
      • 2023-02-17
      • 2012-07-15
      相关资源
      最近更新 更多