UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

68 lines 2.35 kB
/** * Estimates the clock offset between this peer and a remote peer using the * NTP four-timestamp protocol. * * Each round trip provides four timestamps: * t0: local time when the request was sent * t1: remote time when the request was received * t2: remote time when the response was sent * t3: local time when the response was received * * From these: * `RTT = (t3 - t0) - (t2 - t1)` — excludes remote processing time * `offset = ((t1 - t0) + (t2 - t3)) / 2` — remote_time = local_time + offset * * Time units are application-defined; the module just does arithmetic. Callers * typically pass milliseconds. * * Reports the offset from the sample with lowest RTT (the standard SNTP estimator — error on offset is bounded by ±RTT/2, so the lowest-RTT sample is the tightest estimate) * * @author Alex Goldring * @copyright Company Named Limited (c) 2025 */ export class TimeSync { /** * @param {{ window_size?: number }} [options] */ constructor({ window_size }?: { window_size?: number; }); /** * @readonly * @type {number} */ readonly window_size: number; /** * Record a complete four-timestamp sample. * * @param {number} t0 local time at send * @param {number} t1 remote time at receive * @param {number} t2 remote time at send * @param {number} t3 local time at receive */ record(t0: number, t1: number, t2: number, t3: number): void; /** * RTT of the sample whose offset is currently being reported — i.e. the * lowest RTT in the current window. This is the *best-case* round-trip, * not a typical one. Don't use it to size timeouts, jitter buffers, or * render delay; for that, measure observed jitter directly * (see `AdaptiveRenderDelay`). * * @returns {number} */ best_rtt(): number; /** * Estimated `remote_time - local_time`. Add to a local timestamp to get the * estimated remote timestamp. * @returns {number} */ offset(): number; /** * Convert a local timestamp to the corresponding estimated remote timestamp. * @param {number} local_time * @returns {number} */ estimate_remote(local_time: number): number; #private; } //# sourceMappingURL=TimeSync.d.ts.map