CloseNotifier for http ResponseWriter

huangapple go评论145阅读模式
英文:

CloseNotifier for http ResponseWriter

问题

我正在使用Go 1.4.2版本,但是实现中似乎没有CloseNotifier,我想在长轮询处理程序中使用它,类似于:

func Pollhandler(w http.ResponseWriter, r *http.Request) {

    notify := w.(CloseNotifier).CloseNotify()

    <-notify //应该阻塞,直到HTTP连接关闭

}

CloseNotifierhttp.ResponseWriter中没有实现吗?如果是这样,我该如何解决这个问题?或者是否有任何实现了CloseNotifier接口的http.ResponseWriter的实现?

英文:

i am using go 1.4.2 and the implementation doesn't seem to have CloseNotifier as i want to use it in a long polling handler with something like:

func Pollhandler(w http.ResponseWriter, r *http.Request) {

	notify := w.(CloseNotifier).CloseNotify()

    &lt;-notify //should block until the http connection is closed

}

is the CloseNotifier not implemented for the http ResponseWriter? if so how can i get around this? or is there any implementation for http ResponseWriter that implements the CloseNotifier interface?

答案1

得分: 4

http.CloseNotifier自Go 1.1版本以来就存在了(http://golang.org/doc/go1.1)。你的代码不起作用是因为你忘记了包的部分:

notify := w.(http.CloseNotifier).CloseNotify()
英文:

http.CloseNotifier has been there since Go 1.1. Your code doesn't work because you forgot the package part:

notify := w.(http.CloseNotifier).CloseNotify()

huangapple
  • 本文由 发表于 2015年9月18日 20:24:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/32651999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定