Search code examples
javascriptpythongoogle-app-enginechannel-api

Channel Google App Engine channel.open() does not work


I've a problem when opening the channel. i've this on the server side:

   def get(self):
        user = users.get_current_user()
        if not user:
            self.redirect(users.create_login_url(self.request.uri))
            return
        channel_id=str(str(random.randint(0,1000)))
        token = channel.create_channel(channel_id)
        template_values = {
                           'token': token,
                           'me': user.user_id()
                           }

        logger.debug("Token: %s user:%s %s %s" % (token,user.user_id(),user.nickname(),user.email()))
        self.response.out.write(template.render('templates/index.html', template_values))

and this on the HTML (templates/index.html)

<html>
<head>
  <script type="text/javascript" src="/_ah/channel/jsapi"></script>
</head>
<body>
    {{ token }}
    <script>alert("a0");
        var token = {{ token }};
        alert("a1");
        var channel = new goog.appengine.Channel(token);
        alert("a2");
        var socket = channel.open();
        alert("a3");


        socket.onopen = function(){
            alert("open");
        };

        socket.onmessage = function(m){
            var data = $.parseJSON(m.data);
            alert(data)
        };
        socket.onerror =  function(err){
            alert("Error => "+err.description);
        };
        socket.onclose =  function(){
            alert("channel closed");
        };
  </script>
</body>
</html>

I put alert to see if everything works, but a0 a1 a2 are raised, while a3 doesn't. Where is the problem? why channel.open() does not work?

PS: is there any way to trace these errors in javascript ? something more effective then guessing where is the error.


Solution

  • For debugging use either Firebug or the Chrome debugger. You can log messages to the console by adding lines into your Javascript:

    window.console.log("Message")
    

    Double check that the value you get for 'token' is indeed the correct token.