Google AppEngine channel is opened, client is receiving responses, but socket.onmessage is not being called

259 views Asked by At

I have a webpage that I want to use the google app engine channel API with. I have a token being generated with an external library, which is fed into this very, very simple javascript.

<html lang="en">
<body>
    <script src="jquery-1.6.3.min.js" ></script>
    <script type="text/javascript" src="/_ah/channel/jsapi"></script>
    <script type="text/javascript">
        var token, channel, socket;
        var onOpened = function() {
            document.body.innerHTML += "<br>Open!";
        };
        var onMessage = function(msg) {
            document.body.innerHTML += "<br>Message: " + msg;
        };
        var onChannelError = function(error) {
            document.body.innerHTML += "<br>Error! :'(" 
        };
        var onClose = function(e) {
            document.body.innerHTML += "<br>Close :(";
        };
        var handler = {
            onopen: onOpened,
            onmessage: onMessage,
            onerror: onChannelError,
            onclose: onClose
        };
        var openChannel = function(t) {
            token = t;
            channel = new goog.appengine.Channel(token);
            socket = channel.open(handler);
        };
    </script>
</body>
</html>

When I run this code (calling openChannel with my channel token), the onOpen method is called (so the HTML changes ot say "Open!". My var socket ends up looking like this:

rf {readyState: 1, cc: Array[0], onopen: function, onmessage: function, onerror: function…}

And, when I look at the ChromeInspector's network log, after the channel is opened, I can see that the browser is now successfully longpolling (not sure if that's the correct term) talkgadget.google.com. In response, it's getting what looks like perfectly fine responses. I get a lot of numbers and brackets and ["noop"]s in most responses. And if I manually trigger a notification in the server, my client receives the notification information in its request! But my socket.onmessage is still never called!

Here's a screenshot of my network tab at the time. This looks like successful long polling, to me

Manually calling socket.onmessage({}) changes the DOM to say "Message: [object Object]", so my handler doesn't seem to be a problem. And there's a breakpoint there anyway just in case. If I call socket.close(), my onClose function correctly calls, too.

This is driving me insane, so thanks so much for any help or advice you can give me!

2

There are 2 answers

0
thefistopher On BEST ANSWER

I talked with Google about the issue I was having. According to them, the issues arose because in my application, I would modify the DOM whenever I interacted with the channel. For whatever reason, this caused the iframe gadget that is inserted by the channel to silently break. So I removed the DOM manipulation (ie. innerHTML += "Opened Channel") and the channel works.

3
omerio On

We have been using this as well and faced the same issue, this workaround seems to work fine for us, assigning the onmessage after the socket is created:

channel = new goog.appengine.Channel(token);
socket = channel.open();
socket.onmessage = onMessage;