使用 Channel 检查客户端存在
我目前正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您要求的是像channel.enumerate_connected_clients()这样的方法。
没有这样的事。您需要使用数据存储和/或内存缓存来自行跟踪它。如果您只使用数据存储区,则可以执行以下操作:
定义模型:
在创建通道时创建新的客户端实体:
现在在连接或断开连接处理程序中,更新“已连接”属性:
枚举您的客户端实体并显示它们是留给读者的练习。动态更新现有客户列表也是如此。
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:
Create a new client entity when you create the channel:
Now in your connect or disconnect handler, update the 'connected' property:
Enumerating your Client entities and displaying them is an exercise left to the reader. As is dynamically updating the list for existing clients.