doubu7425 2018-09-26 11:54
浏览 139
已采纳

如何为服务器发送事件(SSE,EventStream)配置IIS的HTTPPlatformHandler

Currently I have program that provide SSE as a service, and I have to deploy on IIS. But its does not work correctly, Here is the result when I run .exe without IIS.

data: Hello, world

But when its run behind IIS, Browser was stuck on loading. I have to flush event Hello, world thousand times to make IIS flush result to browser and it's flush instantly instead of incremental update like SSE use to be.

Here is my 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>

Here is my go code

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

"))
        flusher.Flush()
        time.Sleep(time.Millisecond * 100)
    }
}
  • 写回答

1条回答 默认 最新

  • drlma06060 2018-09-28 10:30
    关注

    Actually HttpPlatformHandler has 8kb output buffer , so my message is not sent out immediately.

    I have to change HttpPlatformHandler to ASP.NET Core Module, so web.config must update to this.

        <?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>
    

    And to start go 's application as aspNetCore on iis, the application need to get environment variable name ASPNETCORE_PORT then start http service on that port.

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

    That's all!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法