1 Star 1 Fork 0

qaqtutu/timer-sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Timer Sync

A timer Demo that synchronizes multiple clients using trust.

Online Demo

http://118.24.72.153:8081/

CountDown

CountDown

Timer

CountDown

Principle

Time Synchronization

The client sends time T1, the server sends time T2 after receiving the message, and the client records time T3 after receiving the message.

The network delay between the server and the client can be recorded as Delay=(T3 - T1) / 2.The time difference between the server and the client (different devices have different time) can be recorded as diff_time = T2 + Delay - Now().The client can use diff_time to calculate the current server time.

Status Synchronization

All state attributes of the timer are saved in the server. The client sends a message to modify the state of the timer in the server, and then broadcasts it to all clients.

When the client receives the status information sent by the server, it saves the information and starts to continuously calculate the accumulated time of the timer or the remaining time of the countdown.

mode

Timer mode. Timer/CountDown

state

Running state. Start/Stop.

time

The start time of the timer or the end time of the countdown.

counter

The accumulated time of the timer or the remaining time of the countdown.

Error Analysis

We can see from the figure that the values of the four timers or countdowns displayed in the screenshot are inconsistent. The error mainly comes from two aspects.

Network Delay

We used diff_ Time = T2 + Delay - Now() to calculate the difference between local time and server time for conversion between local time and server time. T2 + Delay represents the current time of the server. Delay represents the difference between the time when the server sends the response and the time when the response arrives at the client. The problem is that Delay cannot be calculated accurately. We can only use (T3 - T1) / 2 to estimate the value of Delay.

Refresh rate

The client refreshes the display value of the timer at a fixed frequency. Different client tasks have different execution times, and the calculated values are different. This error is usually within a dozen milliseconds. You can reduce this error by increasing the refresh rate.

空文件

简介

一个Rust实现的多客户端计时器同步Demo 展开 收起
Rust 等 2 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qaqtutu/timer-sync.git
git@gitee.com:qaqtutu/timer-sync.git
qaqtutu
timer-sync
timer-sync
master

搜索帮助