当前位置: > > > > 如何为服务器发送事件(SSE、EventStream)配置IIS的HTTPPlatformHandler
如何为服务器发送事件(SSE、EventStream)配置IIS的HTTPPlatformHandler
来源:stackoverflow
2024-04-23 09:42:35
0浏览
收藏
哈喽!大家好,很高兴又见面了,我是米云的一名作者,今天由我给大家带来一篇《如何为服务器发送事件(SSE、EventStream)配置IIS的HTTPPlatformHandler》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!
问题内容
目前我有一个提供 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 有 ,所以我的消息没有立即发送出去。
我必须更改, 所以 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 上启动 go 的应用程序作为 aspnetcore ,应用程序需要获取环境变量名称 aspnetcore_port 然后在该端口上启动 http 服务。
port := os.Getenv("ASPNETCORE_PORT")
http.ListenAndServe(":"+port, nil)
仅此而已!
好了,本文到此结束,带大家了解了《如何为服务器发送事件(SSE、EventStream)配置IIS的HTTPPlatformHandler》,希望本文对你有所帮助!关注米云公众号,给大家分享更多Golang知识!
