英文:
Channel api: persistent disconnections and connections
问题
我正在使用Channel API,在终端中打开通道大约1分钟后,我看到以下信息:
INFO 2015-10-20 11:18:08,489 module.py:786] default: "POST /_ah/channel/disconnected/ HTTP/1.1" 200 2278
2015/10/20 11:18:10 handlerMain executed
INFO 2015-10-20 11:18:10,482 module.py:786] default: "POST /_ah/channel/connected/ HTTP/1.1" 200 2279
2015/10/20 11:18:13 handlerMain executed
INFO 2015-10-20 11:18:13,486 module.py:786] default: "POST /_ah/channel/disconnected/ HTTP/1.1" 200 2279
2015/10/20 11:18:14 handlerMain executed
INFO 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
并且头部包含Connection: "keep alive";
当我切换到另一个标签页时,几秒钟后这些请求停止(或者非常缓慢),然后我开始收到这些连接/断开连接的通知。如果我跳回到该页面,请求会再次每秒钟发送一次,而且没有通知。
英文:
I am using Channel API, and after about 1 minute after channel's opening in terminal I see
INFO 2015-10-20 11:18:08,489 module.py:786] default: "POST /_ah/channel/disconnected/ HTTP/1.1" 200 2278
2015/10/20 11:18:10 handlerMain executed
INFO 2015-10-20 11:18:10,482 module.py:786] default: "POST /_ah/channel/connected/ HTTP/1.1" 200 2279
2015/10/20 11:18:13 handlerMain executed
INFO 2015-10-20 11:18:13,486 module.py:786] default: "POST /_ah/channel/disconnected/ HTTP/1.1" 200 2279
2015/10/20 11:18:14 handlerMain executed
INFO 2015-10-20 11:18:14,482 module.py:786] default: "POST /_ah/channel/connected/ HTTP/1.1" 200 2279
and so on..
my .go file
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)
}
}
javascript from my .html file
<script>
channel = new goog.appengine.Channel('{{.token}}');
socket = channel.open();
socket.onopen = onOpened;
socket.onmessage = onMessage;
socket.onerror = onError;
socket.onclose = onClose;
</script>
If it where just simple notifications, I could have handle it, but this disconnections reexecutes my handlerMain() function, and in this function I want to start goroutine which will send messages to client, in this case I will have multiple repetitions of messages in my html page.
Any thoughts?
Is there alternative for Channel API? I know that websockets unfortunately doesn't work with GAE
UPDATE:
In browser I see GET XMLHttpRequests are sent every second to the
http://localhost:8080/_ah/channel/dev?command=poll&channel=237c7242478266a2856d947decce4b55-channel-2105948409-1445426965-123&client=1
with header Connection : "keep alive";
When I switch browser to another tab, after a few seconds these requests are stopped(or very much slowed down), and I start recieve this connection/disconnection notifications. If i jump back to the page, requests are sent again every second, and no notifications.
答案1
得分: 0
通道的连接/断开只出现在我的MacBook上,而不是在PC上。看起来这与应用引擎开发服务器有关,这里有更多详细信息:https://groups.google.com/forum/#!topic/google-appengine-go/dLe2UvzUgdA
英文:
Channels connection/disconnections appears only on my macbook, not on the PC. Looks like it have something to do with app-engine development server, here more details: https://groups.google.com/forum/#!topic/google-appengine-go/dLe2UvzUgdA
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论