0

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!

thefistopher
  • 429
  • 1
  • 5
  • 15
  • I am not able to create a channel. can you please help on this http://stackoverflow.com/questions/34332222/unable-to-create-new-channel-in-javascript-of-channel-api – Sunil Garg Dec 17 '15 at 10:45

2 Answers2

0

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;
omerio
  • 1,186
  • 7
  • 16
  • Hmm, unfortunately this isn't doing anything for me. :/ Any other pieces of code that you think might've helped? – thefistopher Jun 12 '15 at 14:39
  • I can only suggest two things: we seem to have the channel javascript code inside a setTimeout(function(){}, 500); so it's delayed a little bit, try wrapping yours in timeout or document ready function. Second thing double check that the token on the server is the same one on the client, if you can print the one on the server to the logs and do console.log for the token in the Javascript. Let me know if any of these help – omerio Jun 12 '15 at 14:57
  • Actually, weirdly enough, your code works if I get rid of the handler, and it's just channel.open(). Bizarre.. ... But it works! Yay! Thanks. Hopefully google fixes this – thefistopher Jun 12 '15 at 16:05
0

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.

thefistopher
  • 429
  • 1
  • 5
  • 15