36

I'm trying to handle disconnect / connect states using Presence in the Channel API.

Here are some of my code.

app.yaml

handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css))

- url: .*
  script: main.py

inbound_services:
- channel_presence

main.py

class onConnect(webapp.RequestHandler):
  def post(self):
    for user in users:
      users = User.all().fetch(1000)
      client = client_id = self.request.get('from')
      channel.send_message(user.channel,' connected');

class onDisconnect(webapp.RequestHandler):
  def post(self):
    Mainpage()
    for user in users:
      users = User.all().fetch(1000)
      client = client_id = self.request.get('from')
      channel.send_message(user.channel, ' disconnected');

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                     ('/_ah/channel/connected/',onConnect),
                                     ('/_ah/channel/disconnected/',onDisconnect),
                                     ('/chat',handleChat)],
                                     debug=True)

Javascript

<script>
        openChannel = function(){
            var token = '{{ token }}';
            var channel = new goog.appengine.Channel(token);
            var handler = {
                  'onopen': onOpened,
                  'onmessage': onMessage,
                  'onerror': function() {},
                  'onclose': function() {}
                };
            var socket = channel.open(handler);
            socket.onopen = onOpened;
            socket.onmessage = onMessage;

            var chat = document.getElementById('chatinput');
                chat.onkeyup = function(e){
                    if(e.keyCode == 13){
                        sendChat(this.value);
                        this.value = '';
                    }
                }
        }

        sendMessage = function(path, opt_param) {
            if (opt_param) {
                path += '?' + opt_param;
            }
            var xhr = new XMLHttpRequest();
            xhr.open('POST', path, true);
            xhr.send();
        };

        onOpened = function(){
            console.log('Channel Opened');  
            var chatlog = document.getElementById('chatlog');
            var msg = document.createElement('div');
                msg.innerHTML = 'Channel Opened';
                chatlog.appendChild(msg);
            sendMessage('/chat','m='+'A User Joined.');
        }

        onMessage = function(m){
            console.log('Message Recieved');
            var chatlog = document.getElementById('chatlog');
            var msg = document.createElement('div');
            var d = new Date();
                msg.innerHTML = d.toLocaleTimeString() + ': ' + m.data;
                chatlog.appendChild(msg);
        }

        sendChat = function(msg){
            console.log(msg);
            sendMessage('/chat','m='+msg);
        }

        openChannel();
    </script>

Using this code, connnect and disconnect is not triggering when a user closes their browser or whatever.

Are there anything wrong with this code?

Daniel
  • 42,087
  • 4
  • 55
  • 81
AlexCheuk
  • 5,595
  • 6
  • 30
  • 35
  • 1
    Do you actually create a token and initialize client side? I don't see the code of that part in your question. – alex Apr 14 '12 at 09:29
  • What do you see in your logs for `/_ah/channel/connected` and `/_ah/channel/disconnected`? – Nick Johnson Apr 17 '12 at 23:57
  • are you on SDK 1.6.5? Do you notice the problem when they just reload the page? – Anentropic May 21 '12 at 11:59
  • 2
    Did you ever resolve this, I have the same issue? – themaestro Nov 24 '13 at 19:55
  • I see this is from a while ago, but did you ever find the problem? – davidkomer Feb 02 '15 at 12:42
  • Personally (and I can only speak personally about that!), I haven't been all that happy with the Channel API in the past -- while I'm sure this isn't what you'd like to hear, have you tried switching to Firebase, see http://googlecloudplatform.blogspot.com/2014/11/building-mobile-apps-faster-with-firebase.html , http://www.wired.com/2014/11/why-google-acquired-firebase/ , https://www.firebase.com/ ...? I might be wrong but it seems like it's "the new hotness" in this space, and the acquisition by Google seems to suggest a nice future of integration w/Google Cloud Platform...! – Alex Martelli Mar 21 '15 at 01:58
  • 1
    I'm having the same problem. Incomplete code by google. – Stubbies May 17 '15 at 10:34

1 Answers1

1

Yes, route list is wrong. Put ('/', MainPage) in the end of the routes list. From webapp2 URI routing guide:

When the application receives a request, it tries to match each one in order until one matches, and then call the corresponding handler.

Alex Dvoretsky
  • 938
  • 9
  • 21