简体   繁体   English

同时在多个连接的设备上开始倒计时 - Firebase Android

[英]Start a countdown on several connected devices at the same time - Firebase Android

I'm creating an Android application, in which players are connected to Firebase Realtime Database (2 to 4 players per room).我正在创建一个 Android 应用程序,其中玩家连接到 Firebase 实时数据库(每个房间 2 到 4 个玩家)。 If I summarize what my code should do: players can click on a button each in turn.如果我总结一下我的代码应该做什么:玩家可以依次单击一个按钮。 When a player clicks and it's his turn, the app will write (with runTransaction ) on the server an int type (for example "clicked:2" means that player 2 just clicked).当玩家点击并轮到他时,应用程序将(使用runTransaction )在服务器上写入一个 int 类型(例如“clicked:2”表示玩家 2 刚刚点击)。 When the "clicked" key is visible on the server (with addValueEventListener ), all players connected to this room perform (at the same time and the execution is done on the client side) a 3 seconds CountDownTimer .当“点击”键在服务器上可见时(使用addValueEventListener ),连接到该房间的所有玩家(同时在客户端执行)执行 3 秒CountDownTimer During this countdown, all players (without exception) can click on their buttons, and if this is the case only the first one to click will be taken into account (because runTransaction ) and will be written on the server.在这个倒计时期间,所有玩家(无一例外)都可以点击他们的按钮,如果是这种情况,只会考虑第一个点击的人(因为runTransaction )并将写入服务器。 So everyone will see on his device that a player has clicked on his button during the countdown, and then we start all over again (ie it's up to the next player to start the countdown etc).所以每个人都会在他的设备上看到一个玩家在倒计时期间点击了他的按钮,然后我们重新开始(即由下一个玩家开始倒计时等)。 Theoretically, this is fine, but a problem arise after testing (with an old and a new - and therefore faster - smartphone):从理论上讲,这很好,但是在测试后会出现问题(使用旧的和新的 - 因此速度更快 - 智能手机):

Some devices are faster than others, and in most cases the countdown doesn't start at the same time for different players.有些设备比其他设备更快,并且在大多数情况下,不同玩家的倒计时不会同时开始。 So I was thinking, for example (but others are possible, don't hesitate to say so,), to write on the server at what time (in ms) the player clicked.所以我在想,例如(但其他人是可能的,不要犹豫,这样说,),在服务器上写下玩家点击的时间(以毫秒为单位)。 and take this time + 500 ms (or other) to start the countdown.并花这个时间 + 500 毫秒(或其他时间)开始倒计时。 But for that you would need to have Firebase time ( TimeStamp I think) and I don't know how... (preferably only in Java , I don't like to touch json too much)但是为此你需要有 Firebase 时间(我认为是TimeStamp ),我不知道如何......(最好只在 Java 中,我不太喜欢触摸 Z466DEEC76ECDF24FCA6D38571F63)

One of the factors affecting the client-side countdown timers is going to be the latency between each client and the server.影响客户端倒计时的因素之一是每个客户端和服务器之间的延迟。 A client with a faster connection will get the updated value sooner, and hence start the countdown timer sooner.具有更快连接的客户端将更快地获得更新的值,从而更快地启动倒数计时器。

Firebase estimates this latency when the client first connects, and makes it available in the .info/serverTimeOffset node in the client. Firebase 在客户端首次连接时估计此延迟,并使其在客户端的.info/serverTimeOffset节点中可用。 You can use this value to (partially) correct for the latency.您可以使用此值(部分)纠正延迟。 For more on this, and sample code, see the documentation on clock skew .有关这方面的更多信息和示例代码,请参阅有关时钟偏差的文档。

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

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