我正在开发基于 Twitter 标签和服务器发送事件的聊天室,包https://github.com/antage/eventsource
我有关于客户端断开连接的问题。我运行一个 goroutine 向客户端发送消息,但是当客户端断开连接时,goroutine 仍然运行。
我不知道如何在服务器端检测客户端断开连接。
func (sh StreamHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
es := eventsource.New(
&eventsource.Settings{
Timeout: 2 * time.Second,
CloseOnTimeout: true,
IdleTimeout: 2 * time.Second,
Gzip: true,
},
func(req *http.Request) [][]byte {
return [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Access-Control-Allow-Origin: *"),
}
},
)
es.ServeHTTP(resp, req)
go func() {
var id int
for {
id++
time.Sleep(1 * time.Second)
es.SendEventMessage("blabla", "message", strconv.Itoa(id))
}
}()
}
梵蒂冈之花
暮色呼如
相关分类