我正在使用 Channel API,在终端打开通道大约 1 分钟后,我看到INFO 2015-10-20 11:18:08,489 module.py:786] default: "POST /_ah/channel/disconnected/ HTTP/1.1" 200 22782015/10/20 11:18:10 handlerMain executedINFO 2015-10-20 11:18:10,482 module.py:786] default: "POST /_ah/channel/connected/ HTTP/1.1" 200 22792015/10/20 11:18:13 handlerMain executedINFO 2015-10-20 11:18:13,486 module.py:786] default: "POST /_ah/channel/disconnected/ HTTP/1.1" 200 22792015/10/20 11:18:14 handlerMain executedINFO 2015-10-20 11:18:14,482 module.py:786] default: "POST /_ah/channel/connected/ HTTP/1.1" 200 2279等等..我的 .go 文件func init() { http.HandleFunc("/", handlerMain)}func handlerMain(w http.ResponseWriter, r *http.Request) { log.Println("handlerMain executed") c := appengine.NewContext(r) tok, err := channel.Create(c, "123") if err != nil { panic(err) } templ := template.Must(template.ParseFiles("./templates/posts.html")) err = templ.Execute(w, map[string]string{ "token": tok, }) if err != nil { panic(err) } }我的 .html 文件中的 javascript <script> channel = new goog.appengine.Channel('{{.token}}'); socket = channel.open(); socket.onopen = onOpened; socket.onmessage = onMessage; socket.onerror = onError; socket.onclose = onClose; </script>如果它只是简单的通知,我可以处理它,但是这种断开连接会重新执行我的 handlerMain() 函数,并且在这个函数中我想启动 goroutine,它将向客户端发送消息,在这种情况下,我将多次重复消息我的 html 页面。有什么想法吗?Channel API 有替代方案吗?我知道不幸的是 websockets 不适用于 GAE更新:在浏览器中,我看到 GET XMLHttpRequests 每秒发送到http://localhost:8080/_ah/channel/dev?command=poll&channel=237c7242478266a2856d947decce4b55-channel-2105948409-1445426965-123&client=1带有标题连接:“保持活动状态”;当我将浏览器切换到另一个选项卡时,几秒钟后这些请求就会停止(或非常慢),然后我开始收到此连接/断开连接通知。如果我跳回该页面,则每秒都会再次发送请求,并且没有通知。
1 回答
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
通道连接/断开仅出现在我的 macbook 上,而不出现在 PC 上。看起来它与应用引擎开发服务器有关,这里有更多详细信息:https : //groups.google.com/forum/#!topic/ google-appengine-go/ dLe2UvzUgdA
- 1 回答
- 0 关注
- 161 浏览
添加回答
举报
0/150
提交
取消