1
votes

I am currently developing an application on Google App Engine for one of my course project, and am now trying to come up with a side panel that will update itself whenever someone logs in and connect to my application. The purpose is to let other users see who is online, so that they can challenge each other in a game (yes, I am writing a game app). I managed to do this by using the Channel API, enabling channel presence and implementing handlers. It's working perfectly, but it only works when I stay on a page and someone logs in. When I move on to another page, I have no way to check if that user is still connected. The list only updates when someone connects to it, but will not show the users that are currently connected.

Is there a way I can check which users are connected using GAE's Channel? I noticed that a warning message is printed when my app attempt to send a ChannelMessage to a clientId that is not currently connected. Is there anything in the Channel API that can allow me to do the same thing?

1

1 Answers

2
votes

It sounds like what you're asking for is a method like channel.enumerate_connected_clients().

There is nothing like this. You need to use the datastore and/or memcache to track it yourself. If you just use the datastore, you could do something like this:

Define your model:

class Client(db.Model):
  name = db.StringProperty()
  connected = db.BooleanProperty()

Create a new client entity when you create the channel:

  # Create an entity in the database and use its key as the clientid
  client = Client(name=username, connected=False)
  client.put()
  token = channel.create_channel(str(client.key()))

  # Then pass that token down to your client

Now in your connect or disconnect handler, update the 'connected' property:

class ConnectHandler(webapp.RequestHandler):
  def post(self):
    client = Client.get(Key(self.request.get('from')))
    client.connected = True
    client.put()

# Have a similar class for DisconnectHandler, 
# or be more clever by inspecting the path.

Enumerating your Client entities and displaying them is an exercise left to the reader. As is dynamically updating the list for existing clients.