简体   繁体   English

如何确保socket.io问答游戏中的所有玩家同时显示问题?

[英]How do I ensure questions are displayed at the same time for all players in socket.io quiz game?

I'm developing a multiplayer quiz game that runs in the client's browser. 我正在开发在客户端浏览器中运行的多人问答游戏。 It uses socket.io and node.js on server side and javascript on client side. 它在服务器端使用socket.io和node.js,在客户端使用javascript。

Questions are picked from a database and sent to each player at the same time but slower internet connections could mean that they will receive them at different times. 从数据库中选择问题并同时将其发送给每个玩家,但是较慢的Internet连接可能意味着他们将在不同的时间收到问题。

Possible solution: 可能的解决方案:

  • Send a timestamp with each question which tells the browser when to display the question. 为每个问题发送一个时间戳,告诉浏览器何时显示该问题。 The timestamp would be (send time + x seconds) to account for connection lag. 时间戳记为(发送时间+ x秒)以说明连接滞后。 However, if the client knew how to use the console they could intercept the question as soon as it arrives and therefore see it before all the other players. 但是,如果客户知道如何使用控制台,则他们可以在问题到达后立即拦截该问题,因此可以在所有其他玩家之前看到它。

So how could I ensure that no player has an unfair advantage ad receives questions before the others? 那么,如何确保没有一个拥有不公平优势广告的玩家在其他玩家之前收到问题呢?

You cant. 你不能。 What matters is that all players believe they are playing at the same time, that players get the same time to answer and that the result is determined at the sever. 重要的是所有玩家都认为自己正在同时玩耍,玩家有相同的回答时间,并且结果是由服务器决定的。 Nobody gets an advantage as they have the same amount of time to answer and the time to answer is the time between question presentation and answer given. 没有人会获得优势,因为他们有相同的回答时间,而回答的时间就是问题陈述和给出答案之间的时间。 At most they will only be a few seconds apart and clients will be total unaware they are not playing simultaneously. 最多它们之间只有几秒钟的间隔,客户将完全不知道自己没有同时玩。

A possible solution would be store info on a server when the user received questions and user submitted the answer. 一种可能的解决方案是,当用户收到问题并提交答案时,将信息存储在服务器上。 As the addition, you define timeframe in which all user can submit their answers (aka round ). 此外,您还定义了所有用户都可以提交其答案的时间范围(又称回合 )。 You can define winner by smallest diff in timestamps. 您可以按时间戳的最小差异来定义获胜者。 Socket.io delay should not produce any significant problems in the logic as seems you send not so many data and the same time sockets have logic with heartbeats and in case you client will have connection problem - he will be disconnected. Socket.io延迟不会在逻辑上产生任何重大问题,因为似乎您发送的数据并不多,并且同时套接字具有心跳逻辑,并且如果客户端遇到连接问题-他将被断开连接。 To reduce the risk of this logic to be broken you can reduce the time which Socket.io library can consider as the broken connection - pingInterval and pingTimeout can help you with that. 为了减少这种逻辑损坏的风险,您可以减少Socket.io库视为断开连接的时间pingIntervalpingTimeout可以帮助您解决这一问题。

您可以编写服务器端逻辑以仅在所有参与者的登录名和他们的套接字连接均建立时发送问题。.-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM