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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂