Debugging Channel API Appengine

血红的双手。 提交于 2019-12-12 03:57:17

问题


I'm using the appengine channel api(with deferred tasks) but it doesn't seem to be working.

Here's my server code in a gist:

Class Handler(webapp2.RequestHandler):
  def get(self):
    path = jinja_environment.get_template('templates/new_console.html')
    token = channel.create_channel('some_key')
    # Deferring the task.
    deferred.defer(_task, token)
    args = {}
    args['token'] = token
    self.response.out.write(path.render(args))



def _task(token):
  FeedbackThreadModel(id='id').put()
  time.sleep(60)
  channel.send_message(token, 'done')

And here's my javascript client

<html>
  <head>
    <script type="text/javascript" src="/_ah/channel/jsapi"></script>
    <script>
        channel = new goog.appengine.Channel('{{ token }}');
        socket = channel.open();
        socket.onmessage = onMessage;
        onMessage = function() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/secondpage');
            xhr.send();
        };
      </script>
  </head>

I expect the GET request to be initiated to the URL: '/secondpage' after the task is complete, but that's not happening. What am I doing wrong ?


回答1:


This now seems to be working. Apparently the socket request javascript should be in html body and not in head. The working javascript looks like this:

<html>
  <body>
    <script type="text/javascript" src="/_ah/channel/jsapi"></script>
    <script>
        channel = new goog.appengine.Channel('{{ token }}');
        socket = channel.open();
        socket.onmessage = function() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/secondpage');
            xhr.send();
        };
      </script>
  </body>


来源:https://stackoverflow.com/questions/35348050/debugging-channel-api-appengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!