通道 API:持久的断开连接和连接

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

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: &quot;POST /_ah/channel/disconnected/ HTTP/1.1&quot; 200 2278
2015/10/20 11:18:10 handlerMain executed
INFO     2015-10-20 11:18:10,482 module.py:786] default: &quot;POST /_ah/channel/connected/ HTTP/1.1&quot; 200 2279
2015/10/20 11:18:13 handlerMain executed
INFO     2015-10-20 11:18:13,486 module.py:786] default: &quot;POST /_ah/channel/disconnected/ HTTP/1.1&quot; 200 2279
2015/10/20 11:18:14 handlerMain executed
INFO     2015-10-20 11:18:14,482 module.py:786] default: &quot;POST /_ah/channel/connected/ HTTP/1.1&quot; 200 2279

and so on..

my .go file

func init() {
    http.HandleFunc(&quot;/&quot;, handlerMain)
}
func handlerMain(w http.ResponseWriter, r *http.Request) {
            log.Println(&quot;handlerMain executed&quot;)
    
            c := appengine.NewContext(r)
            tok, err := channel.Create(c, &quot;123&quot;)
            if err != nil {
                    panic(err)
            }   
            templ := template.Must(template.ParseFiles(&quot;./templates/posts.html&quot;))
            err = templ.Execute(w, map[string]string{
                    &quot;token&quot;: tok,
            })  
            if err != nil {
                    panic(err)
            }   
 }

javascript from my .html file

  &lt;script&gt;
    channel = new goog.appengine.Channel(&#39;{{.token}}&#39;);
    socket = channel.open();
    socket.onopen = onOpened;
    socket.onmessage = onMessage;
    socket.onerror = onError;
    socket.onclose = onClose;
  &lt;/script&gt;

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&amp;channel=237c7242478266a2856d947decce4b55-channel-2105948409-1445426965-123&amp;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

huangapple
  • 本文由 发表于 2015年10月20日 19:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/33235817.html
匿名

发表评论

匿名网友

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

确定