Debug API Channel API

I am using the appengine api channel (with deferred tasks), but it does not seem to work.

Here's my core server code:

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 is 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 triggered at URL: '/ secondpage' after the task completes, but this will not happen. What am I doing wrong?

0
source share
1 answer

Now it works. Apparently, the javascript socket should be in the html package, and not in the head. 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>
0
source

Source: https://habr.com/ru/post/1618490/


All Articles