gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:21:53 25 4
gpt4 key购买 nike

目前我有提供 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)
}
}

最佳答案

实际上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)

就是这样!

关于go - 如何为服务器发送事件(SSE、EventStream)配置 IIS 的 HTTPPlatformHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52517156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com