开发者

Track online users in real time in order to play a two-player quiz

开发者 https://www.devze.com 2023-03-18 15:00 出处:网络
I am willing to create a two-player quiz game, in real time. For this I need to track when users are on my quiz\'s page and when they leave it. Basically, I need some kind of lobby, where players gath

I am willing to create a two-player quiz game, in real time. For this I need to track when users are on my quiz's page and when they leave it. Basically, I need some kind of lobby, where players gather and from where my script picks two random players who should face each other in the game.

As many players would switch from online to offline in a very short time, I prefer not to use the classic 'update player status to database and then retrieve at a given interval the list of online players'. Currently, I am toying with the idea of using Comet Push, and have studied a little bit PubNub.

The actual quiz game I believe it is pretty easy to solve: use of classic Comet publish() to send answers and subscribe() to check the other user's status. But the thing that bothers me is how do I get that specific list with online users?

I tried to publish() a user_id when a user goes to the lobby, but how do I notify all the other users when this user goes offline? How do I display in the lobby the total number of connected users together with their names?

I开发者_Go百科t's the first time I face Comet and its logic isn't entirely clear to me.

Thank you for your help!


You might want to try out Pusher and our (I work for Pusher) presence functionality. The presence channels let you easily implement room/lobby-style functionality and should meet your criteria.

  • You get an initial list of users subscribed to the channel when you subscribed
  • You are informed whenever a user subscribes (enters) and unsubscribes (leaves) the channel (room/lobby)

This way you don't need to implement this functionality yourself. It's part of the solution Pusher provides.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号