使用 Channel 检查客户端存在

发布于 2024-12-11 03:57:02 字数 387 浏览 0 评论 0原文

我目前正在 Google App Engine 上为我的一个课程项目开发一个应用程序,现在正在尝试开发一个侧面板,每当有人登录并连接到我的应用程序时,该侧面板就会自行更新。目的是让其他用户看到谁在线,以便他们可以在游戏中互相挑战(是的,我正在写一个游戏应用程序)。我设法通过使用 Channel API、启用通道存在并实现处理程序来做到这一点。它工作得很好,但只有当我停留在一个页面上并且有人登录时它才有效。当我转到另一个页面时,我无法检查该用户是否仍然连接。该列表仅在有人连接时更新,但不会显示当前连接的用户。

有没有办法检查哪些用户使用 GAE 的频道连接?我注意到,当我的应用程序尝试将 ChannelMessage 发送到当前未连接的 clientId 时,会打印一条警告消息。 Channel API 中有什么可以让我做同样的事情吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2024-12-18 03:57:03

听起来您要求的是像channel.enumerate_connected_clients()这样的方法。

没有这样的事。您需要使用数据存储和/或内存缓存来自行跟踪它。如果您只使用数据存储区,则可以执行以下操作:

定义模型:

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

在创建通道时创建新的客户端实体:

  # 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

现在在连接或断开连接处理程序中,更新“已连接”属性:

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.

枚举您的客户端实体并显示它们是留给读者的练习。动态更新现有客户列表也是如此。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文