火力地堡倒数计时器同步的在多个客户端 [英] Firebase Countdown Timer Synched Across Multiple Clients

查看:207
本文介绍了火力地堡倒数计时器同步的在多个客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要带一条缝在建设一个一分钱拍卖网站采用了棱角分明的JS特定的利基。我想规划倒数计时器,我一直渴望有原因的尝试火力点。

I am going to take a crack at building a penny auction site for a specific niche using angular JS. I am trying to plan the countdown timers and i've been itching for a reason to try out firebase.

昨天我有想法让每个拍卖不知何故在实际的数据库倒计时,因为2路数据绑定,人民客户方将始终保持更新。

I had the idea yesterday to have each auction somehow have a countdown in the actual database, because with 2 way data binding, peoples clientside will always stay updated.

在该火力点的变化,所有连接的客户端立即改变。所以我的问题是这样的......我怎么能这样做一个特定的记录中的一个服务器端的倒计时。

When the firebase changes, all the connected clients instantly change. So my question is this... How could I do a server side countdown within a particular record.

所以说,我有项x的记录,它有所有的项目信息和阵列关键之一是倒计时:59:01:00。什么是倒计时从59:01:00至00:00:00在服务器端现实和可扩展的方式。

So say I had a record for item x and it had all that items information and one of the array keys is "countdown: 59:01:00". What is a realistic and scalable way to countdown from 59:01:00 to 00:00:00 on the SERVER side.

我想也许运行每隔1秒一个cronjob?但随后数百数据库条目的运行数百个倒计时的每一秒,服务器可能会崩溃。

I was thinking maybe a cronjob that runs every 1 second? But then with hundreds of database entries running hundreds of countdowns each second, the server would probably crash.

什么好的想法?

推荐答案

火力地堡确实可以处理数百个数据库操作每秒(更多的时候)的。但是,这会大大超过设计的。

Firebase could indeed handle hundreds of database operations every second (many more in fact). But this would be greatly over-engineered.

而不是运行一个计时器,每秒更新服务器上的,只是存储的启动/停止活动的时间和允许客户端管理自己的定时器。这不应该被递减1进行每秒(如setInterval的和其他客户端工具不是非常精确的),而是由当前时间戳比较结束时,以及确定的差。

Instead of running a timer that updates every second on the server, simply store the start/stop times of the event and allow the clients to manage their own timers. This should not be done by decrementing by 1 every second (as setInterval and other client-side tools are not very exact) but by comparing the current timestamp to the end, and determining the difference.

timeout = setInterval(countDown, 1000);
function countDown() {
    setTime(Math.max(0, endsAt - Date.now()));
    if( endsAt <= Date.now() ) { clearInterval(timeout); }
}

function setTime(remaining) {
    var minutes = Math.floor(remaining / 60000);
    var seconds = Math.round(remaining / 1000);
    $('#timer').text(lpad(minutes) + ':' + lpad(seconds));
}

最有可能的,你要负责定时器的服务器的原因是使公平。但是,这不符合逻辑。如果每个用户保持自己的股票下调至结束时间,和开始/结束时间是固定的,没有什么在这里砍。

Most likely, the reason you want the server in charge of the timer is to make it "fair." But this isn't logical. If each user keeps their own "ticker" down to the end time, and the start/end time are fixed, there is nothing to hack here.

这篇关于火力地堡倒数计时器同步的在多个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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