2

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.

EsseTi
  • 4,079
  • 5
  • 36
  • 63
  • There's Firebug for Firefox or you can use the built-in Javascript debugger for Chrome, Opera, etc. There's also a Javascript console in most browsers that shows errors. – Alex W Jul 19 '12 at 18:38
  • i thought this as well, but i don't see "open" as alert as well. so the channel seems to not be opened. I'm actually trying to get the /_ah/channel/connected/ data, but i don't really get how it works – EsseTi Jul 19 '12 at 18:41
  • Are you using the Google App Engine documentation? – Alex W Jul 19 '12 at 18:42
  • for what? for the connected/disconnected? i'm reading it right now. i added this `- url: /_ah/channel/connected/.* script: handlers_connect.app` which leads to this class `import webapp2 import logging # create logger logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) class Channel(webapp2.RequestHandler): def post(self): client_id = self.request.get('from') logger.debug("client id %s" % client_id) return app = webapp2.WSGIApplication([('/.*', Channel)], debug=True)` but nothing so far. – EsseTi Jul 19 '12 at 18:45

2 Answers2

3

I had this same problem. It ran fine on local host, but did not work once I uploaded it. It appears that the tokens don't always start formatted correctly, and you need to trim them.

Thus the following alerts with "1", but then crashes:

if(user != null)
{
    $.get("tutorial",{channelKey:userId} ,function(data) {alert(data); token = data;
    alert(userId);
    channel = new goog.appengine.Channel(data);
    alert(1);
    socket = channel.open();
    alert(2);
    socket.onopen = onOpened;
    alert(3);
    socket.onmessage = onMessage;
    alert(4);
    socket.onerror = onError;
    alert(5);
    socket.onclose = onClose;
    alert(6);
});

If you change this to:

if(user != null)
{
    $.get("tutorial",{channelKey:userId} ,function(data) {alert(data); token = data;
    alert(userId);
    channel = new goog.appengine.Channel(data.trim());
    alert(1);
    socket = channel.open();
    alert(2);
    socket.onopen = onOpened;
    alert(3);
    socket.onmessage = onMessage;
    alert(4);
    socket.onerror = onError;
    alert(5);
    socket.onclose = onClose;
    alert(6);
});

However, it works perfectly and opens the channel!

j0k
  • 22,600
  • 28
  • 79
  • 90
1

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.

dragonx
  • 14,963
  • 27
  • 44
  • actually the problem is that i miss the ' ' around {{ token }} but even with this the function for onopen is not raised. – EsseTi Jul 19 '12 at 19:42
  • I'm not sure about this, but the callback might be missed because the browser is waiting for you to click on one of the other asserts. – dragonx Jul 19 '12 at 19:48
  • even removing the alert before the one inside the onOpened is not raised. is it normal that the channel (token) is changed everytime i refresh the page? – EsseTi Jul 19 '12 at 19:51
  • I would say "no, it's not normal", as in, you shouldn't do that. But yes, it's expected in your case because in your get() handler you create a new token on every request. – dragonx Jul 19 '12 at 20:44
  • also, verify that channel.open() is returning with a valid socket object, that might explain why your onopen() is not called. – dragonx Jul 19 '12 at 21:00
  • how can i verify if it is or not a valid socket? – EsseTi Jul 19 '12 at 21:08
  • Use the debugger, do you get a valid object out of channel.open()? – dragonx Jul 19 '12 at 21:22
  • the debugger says ok for the channel but complains about onOpened saying that is not defined (but it's defined!). Actually i was doing smt like this ` socket.onopen = onOpened; onOpened = function() { alert('we are open'); }; ` and this is not working (why?) – EsseTi Jul 19 '12 at 21:36
  • You need to define onOpened first before assigning to it. If you post your actual code, it would be much easier to tell. Anyways, your original question has been answered. – dragonx Jul 20 '12 at 01:17
  • If you aren't already using Chrome as your browser, I highly recommend it for debugging Javascript. Here's the documentation for the debugger which is built in, it makes debugging issues like yours pretty easy: https://developers.google.com/chrome-developer-tools/docs/overview. I can see how it would be frustrating debugging with only alerts. That's very 2002. – dragonx Jul 20 '12 at 14:05