HTML and CSS Reference
Synchronized Time Among Clients
To deal with latency effectively, all clients and the server must have the same absolute time. If a client's operating system
clock is different from the server's clock, the client will either send his or her messages too late or try to simulate too far into
the future. In both cases, the client's experience will be choppy and could affect the other clients in the system. To address
this, a Network Time Protocol ( NTP ) synchronization process can be added to calculate a time that all computers can agree
on. Two approaches to NTP are to sync with the server's time or to sync with a third-party NTP server. One library that
implements a simple NTP client and server is Socket-NTP ( https://github.com/calvinfo/socket-ntp ).
Fast-Forwarding the Game State
With client-side prediction, there can be times when the server sends the client a copy of the game state that is older
than the current time. In this case, the client must fast-forward the game state by playing many frames of the game
engine in rapid succession. To fast-forward from a past state, it is important that each client keep a history of actions
received since the last received game state. During fast-forwarding, the client replays actions experienced in the past.
Figure 12-3 illustrates fast-forwarding. In the first image the client has predicted a car moving in a straight line.
The second image depicts a new state from the server, in which the car has made a left turn. Because the left turn event
took place before the current time of the predicted state, the client must go back in time, apply the left turn, and then
fast-forward in time to catch up to the predicted time. This ensures that the player's perception of time always moves in
one direction. Note that, when rewinding or fast-forwarding, the user interface (i.e., graphics, sound, and so on) are not
updated, so the player does not experience the rewind or fast-forward, but only the causal effects of these actions.
Figure 12-3. Fast-Forwarding the game state