unable to create new channel in javascript of channel API

Here is my python file

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()

and HTML file

<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>

On the page there is Open Connection button that create the channel to the created token on server side. Now when I click on this button it flashes the error alert and then close alert. I want to make a channel and send message from server to the browser.

Am I missing something ?

Answers:

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.