dongyanju1094 2015-10-20 11:46
浏览 52
已采纳

通道API:持续断开连接和连接

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条回答 默认 最新

  • douqiaolong0528 2015-10-29 19:01
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页