通道 api:持续断开和连接

Channel api: persistent disconnections and connections

我正在使用频道 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)
            }   
 }

javascript 来自我的 .html 文件

  <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

与 header 连接:"keep alive";

当我将浏览器切换到另一个选项卡时,几秒钟后这些请求停止(或速度大大降低),我开始收到此 connection/disconnection 通知。如果我跳回页面,请求每秒再次发送,没有通知。

Channels connection/disconnections 只出现在我的 macbook 上,不出现在 PC 上。看起来它与应用程序引擎开发服务器有关,这里有更多详细信息:https://groups.google.com/forum/#!topic/google-appengine-go/dLe2UvzUgdA