0
votes

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
I am not able to create a channel. can you please help on this stackoverflow.com/questions/34332222/…Sunil Garg

2 Answers

0
votes

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;
0
votes

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.