Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle-App-Engine Channel API: Server doesn't send message to client
    primarykey
    data
    text
    <p>I've created simple code to understand, how Google App Engine Channel API works. However, actually my code doesn't work and I dont know why. I am not very experienced in Python so pardon me, if it will be some stupid mistake.</p> <p>Server:</p> <pre><code>from google.appengine.api import channel import webapp2 import jinja2 import os import time channel_key = 'key' class MainHandler(webapp2.RequestHandler): def get(self): token = channel.create_channel(channel_key) template_values = {'token': channel_key} template = env.get_template('index.html') self.response.write(template.render(template_values)) class OpenedHandler(webapp2.RequestHandler): def post(self): channel.send_message(channel_key, "hi") env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__))) app = webapp2.WSGIApplication([ ('/', MainHandler), ('/opened', OpenedHandler) ], debug=True) </code></pre> <p>Client:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;script&gt; var token = "{{ token }}"; onOpened = function() { var xhr = new XMLHttpRequest(); xhr.open('POST', '/opened'); xhr.send(); }; onMessage = function(message) { alert("something recieved"); alert(message); } &lt;/script&gt; &lt;script type="text/javascript" src="/_ah/channel/jsapi"&gt;&lt;/script&gt; &lt;script&gt; channel = new goog.appengine.Channel(token); socket = channel.open(); socket.onopen = onOpened; socket.onmessage = onMessage; socket.onerror = function(e){ alert("error:"+e['description']); }; socket.onclose = function(){ alert("close"); }; &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Javascript console says, that it was successfully sended to /opened, but then no alert comes, like it havent responded. Do you have any idea, where the problem is?</p> <p>Thanks Toneks</p> <p><strong>EDIT:</strong> I added onerror and onclose event handlers to javascript and immediately after opnening the page it calls onerror event saying "Invalid+token" and then onclose event.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload