Google App Engine Channel API javascript location

2019-04-16 08:53发布

I'm a bit confused about this, and want to make sure I'm not being naive about how this works. Does the client side javascript have to also be hosted on Google App Engine? Say I create a channel on my dev server, then I have a local HTML file (non-hosted) on my computer with the required javascript, and I connect to that channel with a token - will that work? Or is this not how channels work?

Edit:

All I have is an HTML file in the same directory as my app.yaml file (so the root directory for my website). I'm in the devserver.

First I create a channel and get the token:

token = channel.create_channel('1')                      
print token                                                    
>>> channel-4132644671-1352248413-1

Then I copy that token in my HTML file:

<html>
<head> 
<script type="text/javascript" src="http://localhost:8080/_ah/channel/jsapi"></script>
</head>
<body >
<script>

    var token = 'channel-4132644671-1352248413-1';
    var channel = new goog.appengine.Channel(token);
    var socket = channel.open();
    socket.onopen = function() { alert('open'); };
    socket.onmessage = function() { alert('message'); };
    socket.onerror = function() { alert('error'); };
    socket.onclose = function() { alert('close'); };        
</script>
</body>
</html>

I open the HTML file with Safari. I get an alert saying "open". However, no matter what token I type in var token, I get an "open" alert, so I'm not sure if getting that alert means anything.

Then I do: channel.send_message('1', 'hi')

And nothing happens in my HTML file. No alerts. What am I doing wrong?

2条回答
Root(大扎)
2楼-- · 2019-04-16 09:18

Because of the same origin policy, the script must be hosted on the same domain as the server that the app opens the channel to. With the present implementation, that server is talkgadget.google.com and the supporting script is https://talkgadget.google.com/talkgadget/channel.js. With curl you can see that the /_ah/channel/jsapi endpoint of your app simply issues a 302 redirect to that script. So unless you intend to develop and run your own channel server, no, you can't do this.

Furthermore, if the underlying implementation of channels should happen to change in the future, App Engine would be updated to redirect /_ah/channel/jsapi to a new script so existing apps would continue to work, while a custom approach would likely break. One less reason to do it yourself.

查看更多
倾城 Initia
3楼-- · 2019-04-16 09:19

In the dev_appserver, the channel is implemented by a javascript function that's constantly polling the server.

If your dev_appserver isn't actually running (it looks like you've somehow broken into dev_appserver), the polling function isn't going to succeed and you won't get the channel messages.

On production, it looks like the channel API uses some sort of long polling.

Also, I'd have to double check the docs, but I believe the first param to send_message should be the token.

查看更多
登录 后发表回答