如何将后端计时器与移动应用程序同步 [英] How to synchronize backend timer with mobile app

查看:45
本文介绍了如何将后端计时器与移动应用程序同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个选择用户并有 15 秒时间的应用.该用户响应的计时器.用户应用程序每 5 秒查询一次数据库以查看是否选择了该用户.如果是这样 移动应用程序开始 15 秒.计时器.问题是计时器永远不会匹配,因为用户应用程序可能处于与后端不同的计时器周期,并且可以稍后查询数据库.我使用 Angular、NodeJS 和 expressJS + MongoDB 来开发这个应用程序.

I am developing a app that chooses a user and has a 15 sec. timer for that user to respond. The user app queries the db every 5 sec to see if that user is chosen. If so The mobile app begins a 15 sec. timer. The problem is that the timers will never match up because the user app can be on a different timer cycle that the backend and can query the db at a later time. I use Angular, NodeJS, and expressJS + MongoDB to develop this app.

关于如何使计时器同步的任何建议?

any suggestion on how I can get the timers to be synchronized?

推荐答案

您不需要同时运行两个计时器.在前端启动一个计时器,然后让后端知道前端的计时器何时启动.只要将一侧作为唯一的事实来源,另一侧就能推断出计时器是否已完成.

You don't need to have two timers running at the same time. Start a timer on the front-end, then let the back-end know when the timer on the front-end started. As long as you treat one side as the single source of truth, the other side will be able to infer whether the timer has finished or not.

虽然定时器可能不同步,但时间在任何地方都应该相同.如果后端知道前端定时器在 12:01:00 开始,它也知道定时器将在 12:01:15 结束.这样就可以继续检查当前时间是在12:01:15之前还是之后.

Although the timers might not be synchronized, time should be the same everywhere. If the back-end knows that the front-end timer started at 12:01:00, it also knows that the timer will end at 12:01:15. In this way, it can just continue to check whether the current time is before or after 12:01:15.

这篇关于如何将后端计时器与移动应用程序同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆