无法在渠道API的javascript中创建新频道

时间:2015-12-17 10:27:09

标签: javascript python google-app-engine channel-api

这是我的python文件

import os
from google.appengine.api import channel
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import logging


token = 'default'

class SendMessage(webapp.RequestHandler):
    def post(self):
        logging.info('Sending messgae with tokens : ' + token)
        channel.send_message(token,'Hello from the server')

class MainPage(webapp.RequestHandler):
    def get(self):
        global token
        token = channel.create_channel('ram')
        logging.info('Token : ' + token)
        channel.send_message(token,'Hello from the server')
        template_values = {'token': token}
        path = os.path.join(os.path.dirname(__file__), 'index.html')

        self.response.out.write(template.render(path, template_values))



application = webapp.WSGIApplication([
    ('/', MainPage),('/sendmessage',SendMessage)], debug=True)


def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

和HTML文件

<html>
    <head> 
        <script type="text/javascript" src="https://talkgadget.google.com/talkgadget/channel.js"></script>
    </head>
    <body >
        <script>
            var state = {
                token: '{{ token }}'
            };
            var token = state.token;
            openChannel = function () {
                var channel = new goog.appengine.Channel(token.toString());
                var socket = channel.open();
                socket.onopen = function () {
                    alert('open');
                };
                socket.onmessage = function () {
                    alert('message');
                };
                socket.onerror = function () {
                    alert('error');
                };
                socket.onclose = function () {
                    alert('close');
                };
            };

            sendMessage = function () {

                var xhr = new XMLHttpRequest();
                xhr.open('POST', '/sendmessage', true);
                xhr.send();
            };
        </script>
        Hi All, <br>
        <button onclick="openChannel()">Open Connection</button>
        <button onclick="sendMessage()">Send Message</button>
    </body>
</html>

在页面上有“打开连接”按钮,用于为服务器端创建的令牌创建通道。现在,当我单击此按钮时,它会闪烁错误警报,然后关闭警报。我想建立一个频道并从服务器向浏览器发送消息。

我错过了什么吗?

0 个答案:

没有答案