I posted this question on StackOverflow 
(http://stackoverflow.com/questions/39254813/appengine-channel-api-duplicate-messages-client-side)
 
but did not get any response. Can someone here shed some light? 

Question reproduced here for convenience:

====

I am trying to use the Channel API to push updates from server to the 
client. The flow is the user presses a button which triggers a server side 
action that generates a lot of logs. I want to display the logs to the user 
"in real time".

When I first load the page it I get all the messages, no problem. If I 
trigger the action a second time without refreshing the page in my browser, 
then all messages appear twice. Here is the set up portion of the channel 
that is tied to the page onLoad event. With resulting console logs I 
gathered that the onMessage() method is being invoked more than once when 
the page is not refreshed. Looks like I need to "kill" earlier sockets in 
some way, but could not find a way in the official documentation. Can 
someone point me in the right direction to get rid of the spurious messages?


// First fetch a token for the async communication channel and
// create the socket
$.post("/app/channels", {'op':'fetch', 'id' : nonce},
   function (data, status, xhr) {
       if (status == "success") {
       data = JSON.parse(data);
       token = data["token"];
       console.log("Cookie: " + get_mp_did() + "; token: " + token);
       var channel = new goog.appengine.Channel(token);
       var handler = {
           'onopen': onOpened,
           'onmessage': onMessage,
           'onerror': function() {
               $("#cmd_output").append('Channel error.<br/>');
           },
           'onclose': function() {
           $("#cmd_output").append('The end.<br/>');
           $.post("/app/channels", {'op':'clear'});
           }
       };
       var socket = channel.open(handler);
       socket.onopen = onOpened;
       socket.onmessage = onMessage;
       }
   });

onOpened = function() {
$("#cmd_output").empty();
};

onMessage = function(data) {
message = JSON.parse(data.data)['message'];
$("#cmd_output").append(message);
console.log('Got this sucker: ' + message);
}

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/3ac8e384-43da-4ad2-8107-5c8e651b9f44%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to