Go ResponseWriter似乎在我需要使用它之前已关闭
我正在尝试制作bayeux服务器,并将此包用作参考: 几年没碰过,也没完成,但有很多东西。我遇到的问题是,我存储了一个http.responseWriter供以后使用Go ResponseWriter似乎在我需要使用它之前已关闭,go,Go,我正在尝试制作bayeux服务器,并将此包用作参考: 几年没碰过,也没完成,但有很多东西。我遇到的问题是,我存储了一个http.responseWriter供以后使用 func NewLongPollClient(id string, resp http.ResponseWriter, req *http.Request, server Server) Client { client := &longPollClient{ resp, req, make(chan
func NewLongPollClient(id string, resp http.ResponseWriter, req *http.Request, server Server) Client {
client := &longPollClient{
resp,
req,
make(chan http.ResponseWriter),
baseClient{
id,
server,
make(map[string]channel.Channel),
&sync.Mutex{},
make(chan interface{}),
make(chan struct{}),
logger,
},
}
// go client.IncomingLoop()
go client.OutgoingLoop()
go func(c *longPollClient, resp http.ResponseWriter) {
c.respChan <- resp
}(client, resp)
return client
}
func (c *longPollClient) OutgoingLoop() {
for {
resp := <-c.respChan
select {
case msg := <-c.responses:
output, err := json.Marshal(&[]interface{}{msg})
if err != nil {
http.Error(resp, "Error Formatting Response", 406)
c.Close()
return
}
resp.Write(output)
case <-c.done:
return
}
}
}
func NewLongPollClient(id字符串,resp http.ResponseWriter,req*http.Request,server-server)客户端{
客户端:=&longPollClient{
分别,
请求,
make(chan http.ResponseWriter),
基本客户机{
身份证件
服务器,
make(映射[string]channel.channel),
&sync.Mutex{},
make(chan接口{}),
make(chan结构{}),
记录器,
},
}
//转到client.IncomingLoop()中
go client.OutgoingLoop()的
go func(c*longPollClient,resp http.ResponseWriter){
c、 respChan http ResponseWriter将在处理程序返回时关闭。一旦请求处理程序返回,您就不能让goroutine写入它。公平地说,您知道如何管理长轮询场景吗?请求不会立即有响应吗?是的,在处理完之前不要从处理程序返回。调用receive/write直接从处理程序的goroutine循环,而不是启动新的goroutine。超时也会有问题。通过谷歌搜索“go http long polling”应该会产生一些不错的点击率。。。