Skip to content

Commit

Permalink
Merge pull request online-go#2382 from GreenAsJade/use_gobansocket_pi…
Browse files Browse the repository at this point in the history
…ngs_only

Get rid of pinging from UI code, take latency update from GobanSocket
  • Loading branch information
anoek authored Oct 2, 2023
2 parents 22f71a1 + 51a347d commit 0d42ec4
Showing 1 changed file with 21 additions and 46 deletions.
67 changes: 21 additions & 46 deletions src/lib/sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/

import Debug from "debug";
import { GobanSocket, niceInterval, protocol } from "goban";
import { GobanSocket, protocol } from "goban";

const debug = new Debug("sockets");

export const socket = new GobanSocket(window["websocket_host"] ?? window.location.origin);

socket.options.ping_interval = 10000;

export let ai_host;
if (
window.location.hostname.indexOf("dev.beta") >= 0 &&
Expand Down Expand Up @@ -59,73 +61,46 @@ export const ai_socket = ai_host
? new GobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>(ai_host)
: undefined;

let connect_time = Date.now();
if (ai_socket) {
ai_socket.options.ping_interval = 20000;
}

let last_clock_drift = 0.0;
let last_latency = 0.0;
let connect_time = null;

socket.on("connect", () => {
debug.log("Connection to server established.");
socket.send("hostinfo", {});
connect_time = Date.now();
});

socket.on("HUP", () => window.location.reload());
socket.on("hostinfo", (hostinfo) => {
debug.log("Termination server", hostinfo);
//console.warn("Termination server", hostinfo);
});

let last_clock_drift = 0.0;
let last_latency = 0.0;
let last_ai_latency = 0.0;
socket.on("latency", (latency, drift) => {
last_latency = latency;
last_clock_drift = drift;
//console.log("latency update", latency, drift);
});

/* Returns the time in ms since the last time a connection was established to
* the server. */
* the server.
* (Zero if we've never connected)
*/
export function time_since_connect() {
return Date.now() - connect_time;
return connect_time ? Date.now() - connect_time : 0;
}

function ping() {
if (socket.connected) {
socket.send("net/ping", {
client: Date.now(),
drift: last_clock_drift,
latency: last_latency,
});
}
}
function handle_pong(data) {
const now = Date.now();
const latency = now - data.client;
const drift = now - latency / 2 - data.server;
last_latency = latency;
last_clock_drift = drift;
(window as any)["latency"] = last_latency;
}
export function get_network_latency(): number {
return last_latency;
}
export function get_clock_drift(): number {
return last_clock_drift;
}
socket.on("net/pong", handle_pong);
socket.on("connect", ping);
niceInterval(ping, 10000);

function ai_ping() {
if (ai_socket && ai_socket.connected) {
ai_socket.send("net/ping", {
client: Date.now(),
drift: last_clock_drift,
latency: last_ai_latency,
});
}
}
function ai_handle_pong(data) {
const now = Date.now();
const latency = now - data.client;
last_ai_latency = latency;
}

ai_socket?.on("connect", ai_ping);
ai_socket?.on("net/pong", ai_handle_pong);
niceInterval(ai_ping, 20000);

export default {
socket: socket,
Expand Down

0 comments on commit 0d42ec4

Please sign in to comment.