Use Channel API to receive messages from task in T

2019-07-22 14:58发布

I am trying to figure out the Task Queue and Channel API.

I have a process that is expected to run >60 seconds added to the task queue when the user submits a form. This process should then send messages through the channel api as it executes. Of coarse I cannot seem to make this work. Can anyone tell me why the SendMessagesHandler is not able to send messages to the client side javascript? Or perhaps the client side javascript is not receiving properly? Also, I do not see the logging messages from SendMessagesHandler in the Log Console, it only shows the run of the mill INFO ... GET or POST. As I write this I am realizing that I must have more than one issue going on here, the task does get created in the task queue, I can see it in the SDK console, however it seems to just keep running endlessly, I would expect it to run for roughly 1m2sec. If I take out the sleep commands from SendMessageHandler it runs to quickly to even catch it in the SDK console.

Thanks in advance for your help.

Here is the main.py file:

#!/usr/bin/env python
import webapp2
import os
import jinja2
import logging
from time import sleep
from google.appengine.api import taskqueue, users, channel
from google.appengine.ext import db

class MainHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            key = self.request.get('key')
            if not key:
                key = user.user_id()
            key_link = 'http://localhost:8080/?key=' + key
            token = channel.create_channel(key)
            template_values = {'token': token,
                    'me': user.user_id(),
                    'key': key,
                    'key_link': key_link,
                    'initial_message': 'Nothing to show yet'}
            template = jinja_environment.get_template('template.html')
            self.response.out.write(template.render(template_values))
        else:
            self.redirect(users.create_login_url(self.request.uri))

    def post(self):
        key = self.request.get('key')
        taskqueue.add(url='/sendmessages', params={'key': key})
        self.redirect('/')

class SendMessagesHandler(webapp2.RequestHandler):
    def post(self):
        key = self.request.get('key')
        sleep(2)
        channel.send_message(key, 'Starting to send messages...')
        logging.info('Starting to send messages...')
        i = 0
        while i < 60:
            i += 1
            logging.info('Counter incremented.')
            channel.send_message(key, 'Counter incemented.')
            sleep(1)

jinja_environment = jinja2.Environment(
        loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
app = webapp2.WSGIApplication([
        ('/', MainHandler),
        ('/sendmessages', SendMessagesHandler)
    ], debug=True)

and here is the template.html

<html>
    <head>
        <title>Simple Task Queue Channel API</title>
        <script src='/_ah/channel/jsapi'></script>
    </head>
    <body>
        <script type='text/jacascript'>
            onOpened = function() {};
            onMessage = function(message) {
                var messageBox = document.getElementById("messageBox");
                messageBox.innerHTML = message;
            };
            onError = function() {};
            onClose = function() {};
            channel = new goog.appengine.Channel('{{token}}');
            socket = channel.open();
            socket.onopen = onOpened;
            socket.onmessage = onMessage;
            socket.onerror = onError;
            socket.onclose = onClose;
        </script>
        <form method="post" action="/">
            <input type="text" name="key" value="{{ key }}">
            <input type="submit" name="submit" value="Receive Messages">
        </form>
        <div id="messageBox"><!-- message should go in here --></div>
    </body>
</html>

Here is the view source from the browser:

<html>
<head>
    <title>Simple Task Queue Channel API</title>
    <script src='/_ah/channel/jsapi'></script>
</head>
<body>
    <script type='text/jacascript'>
        onOpened = function() {};
        onMessage = function(message) {
            var messageBox = document.getElementById("messageBox");
            messageBox.innerHTML = message;
        };
        onError = function() {};
        onClose = function() {};
        channel = new goog.appengine.Channel('channel-2840733866-1367208241-185804764220139124118');
        socket = channel.open();
        socket.onopen = onOpened;
        socket.onmessage = onMessage;
        socket.onerror = onError;
        socket.onclose = onClose;
    </script>
    <form method="post" action="/">
        <input type="text" name="key" value="185804764220139124118">
        <input type="submit" name="submit" value="Receive Messages">
    </form>
    <div id="messageBox"><!-- message should go in here --></div>
</body>
</html>

1条回答
爷的心禁止访问
2楼-- · 2019-07-22 15:26

I spot at least two errors:

First

<script type='text/javascript'>

Second

onMessage = function(message) {
        var messageBox = document.getElementById("messageBox");
        messageBox.innerHTML = message.data;
    };
查看更多
登录 后发表回答