mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
Route gameCommand traffic through WebRTC unreliable DataChannel
Socket.IO (TCP) holds back later packets while it retransmits a lost one, which stalls worldUpdate delivery on lossy long-distance links — exactly the pattern game state suffers worst from. WebRTC DataChannels in unreliable mode (ordered:false, maxRetransmits:0) drop late packets instead of queueing them, which is what we want for high-frequency state sync. Adds a per-user WebRTCTransport on top of the existing Socket.IO connection. Socket.IO stays in charge of bootstrap, signaling (SDP/ICE exchange), and control messages — only gameCommand payloads get routed onto the unreliable channel once it's open. If WebRTC fails to negotiate, gameCommand transparently falls back to Socket.IO, so the game keeps working unchanged. A new StatsLogger writes per-session JSONL events (session_start, webrtc_ready with negotiation time, per-second stats with transport, RTT samples, recv/send rates, seq gaps) so we can compare real-world runs (e.g. Germany server <-> Korea client) instead of guessing. URL flag ?webrtc=0 forces fallback for A/B testing. scripts/webrtc-browser-test.js spins up a headless Chromium against a freshly-started server and asserts the unreliable channel opens and gameCommand traffic actually rides it.
This commit is contained in:
parent
47faae81e5
commit
a0481ed867
9 changed files with 1412 additions and 138 deletions
38
app/Server/StatsLogger.js
Normal file
38
app/Server/StatsLogger.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
define([
|
||||
"fs",
|
||||
"path"
|
||||
],
|
||||
|
||||
function (fs, path) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var runsDir = path.join(process.cwd(), "runs");
|
||||
if (!fs.existsSync(runsDir)) {
|
||||
fs.mkdirSync(runsDir, { recursive: true });
|
||||
}
|
||||
|
||||
var runFile = path.join(
|
||||
runsDir,
|
||||
new Date().toISOString().replace(/[:.]/g, "-") + ".jsonl"
|
||||
);
|
||||
var location = process.env.POC_LOCATION || "unknown";
|
||||
|
||||
fs.writeFileSync(runFile, JSON.stringify({
|
||||
type: "run_start",
|
||||
t: Date.now(),
|
||||
location: location,
|
||||
pid: process.pid
|
||||
}) + "\n");
|
||||
console.log("[stats] logging to " + runFile);
|
||||
|
||||
return {
|
||||
log: function (obj) {
|
||||
obj.t = obj.t || Date.now();
|
||||
obj.location = obj.location || location;
|
||||
fs.appendFile(runFile, JSON.stringify(obj) + "\n", function () {});
|
||||
},
|
||||
getFile: function () { return runFile; },
|
||||
getLocation: function () { return location; }
|
||||
};
|
||||
});
|
||||
|
|
@ -1,10 +1,12 @@
|
|||
define([
|
||||
"Game/Core/User",
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Server/WebRTCTransport",
|
||||
"Server/StatsLogger"
|
||||
],
|
||||
|
||||
function (Parent, ProtocolHelper, nc) {
|
||||
function (Parent, ProtocolHelper, nc, WebRTCTransport, StatsLogger) {
|
||||
|
||||
"use strict";
|
||||
|
||||
|
|
@ -16,39 +18,75 @@ function (Parent, ProtocolHelper, nc) {
|
|||
this.channelPipe = null;
|
||||
this.options = null;
|
||||
|
||||
var self = this;
|
||||
|
||||
socketLink.on('message', this.onMessage.bind(this));
|
||||
socketLink.on('disconnect', this.onDisconnect.bind(this));
|
||||
|
||||
nc.on(nc.ns.server.events.controlCommand.user + this.id, this.socketLink.send, this.socketLink);
|
||||
// Outbound messages from the rest of the server land here. We decide
|
||||
// whether to send over Socket.IO (control) or the WebRTC unreliable
|
||||
// channel (gameCommand, when ready).
|
||||
nc.on(nc.ns.server.events.controlCommand.user + this.id, this.sendOutbound, this);
|
||||
|
||||
// Spin up the WebRTC peer immediately. Negotiation happens via the
|
||||
// socket signaling messages once the client posts its offer.
|
||||
this.webrtc = new WebRTCTransport(this.id, {
|
||||
onLocalDescription: function (desc) {
|
||||
self.socketLink.send(ProtocolHelper.encodeCommand("webrtcAnswer", desc));
|
||||
},
|
||||
onLocalCandidate: function (cand) {
|
||||
self.socketLink.send(ProtocolHelper.encodeCommand("webrtcIce", cand));
|
||||
},
|
||||
onReady: function (negotiationMs) {
|
||||
console.log("[webrtc] " + self.id + " unreliable channel open (" + negotiationMs + "ms)");
|
||||
StatsLogger.log({
|
||||
type: "webrtc_ready",
|
||||
session: self.id,
|
||||
dtMs: negotiationMs
|
||||
});
|
||||
},
|
||||
onMessage: function (raw) {
|
||||
// Inbound gameCommand from client over the unreliable channel.
|
||||
// Same shape as if it came over Socket.IO: a JSON-encoded
|
||||
// gameCommand envelope. Feed it to applyCommand the same way.
|
||||
try {
|
||||
var parsed = JSON.parse(raw);
|
||||
if (parsed.gameCommand !== undefined) {
|
||||
self.onGameCommand(parsed.gameCommand);
|
||||
} else {
|
||||
// Future: other unreliable-channel message types.
|
||||
ProtocolHelper.applyCommand(raw, self);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("[webrtc] bad inbound message:", e.message);
|
||||
}
|
||||
},
|
||||
onClosed: function () {
|
||||
console.log("[webrtc] " + self.id + " unreliable channel closed");
|
||||
}
|
||||
});
|
||||
|
||||
StatsLogger.log({
|
||||
type: "session_start",
|
||||
session: this.id,
|
||||
hasWebrtc: true
|
||||
});
|
||||
}
|
||||
|
||||
User.prototype = Object.create(Parent.prototype);
|
||||
|
||||
/*
|
||||
User.prototype.setChannelPipe = function(channelPipe) {
|
||||
if(channelPipe) {
|
||||
if (channelPipe.isWithinUserLimit()) {
|
||||
this.channelPipe = channelPipe;
|
||||
this.channelPipe.addUser(this);
|
||||
} else {
|
||||
var message = ProtocolHelper.encodeCommand("joinError", {message:"Channel is full"});
|
||||
this.socketLink.send(message);
|
||||
}
|
||||
|
||||
} else {
|
||||
var message = ProtocolHelper.encodeCommand("joinError", {message:"Channel not found"});
|
||||
this.socketLink.send(message);
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
// Socket callbacks
|
||||
// ---------- Inbound from Socket.IO ----------
|
||||
|
||||
User.prototype.onMessage = function (message) {
|
||||
ProtocolHelper.applyCommand(message, this);
|
||||
}
|
||||
};
|
||||
|
||||
User.prototype.onDisconnect = function () {
|
||||
if (this.webrtc) {
|
||||
this.webrtc.destroy();
|
||||
this.webrtc = null;
|
||||
}
|
||||
StatsLogger.log({ type: "session_end", session: this.id });
|
||||
|
||||
if(!this.channelPipe) {
|
||||
console.warn("Disconnecting user without a channel. (Maybe channel was full)");
|
||||
|
|
@ -56,11 +94,32 @@ function (Parent, ProtocolHelper, nc) {
|
|||
}
|
||||
|
||||
this.channelPipe.removeUser(this);
|
||||
};
|
||||
|
||||
|
||||
// ---------- Outbound routing ----------
|
||||
|
||||
// Decides whether a server-generated message should ride Socket.IO or the
|
||||
// unreliable WebRTC channel. gameCommand traffic uses WebRTC when ready;
|
||||
// everything else stays on Socket.IO.
|
||||
User.prototype.sendOutbound = function (message) {
|
||||
if (this.webrtc && this.webrtc.isReady() && isGameCommandMessage(message)) {
|
||||
if (this.webrtc.send(message)) return;
|
||||
// Send failed (channel state changed mid-send) — fall through.
|
||||
}
|
||||
this.socketLink.send(message);
|
||||
};
|
||||
|
||||
function isGameCommandMessage(message) {
|
||||
// The protocol wraps every message as {"<command>": <payload>}; we only
|
||||
// peek at the first key without fully parsing the inner payload.
|
||||
if (typeof message !== "string") return false;
|
||||
// Cheap heuristic — robust because the envelope key is always first.
|
||||
return message.indexOf('{"gameCommand"') === 0;
|
||||
}
|
||||
|
||||
|
||||
// User command callbacks
|
||||
// Remember: control commands are coordinator relevant commands
|
||||
// ---------- Command callbacks (received from client) ----------
|
||||
|
||||
User.prototype.onJoin = function(options) {
|
||||
|
||||
|
|
@ -83,14 +142,12 @@ function (Parent, ProtocolHelper, nc) {
|
|||
var userOptions = {
|
||||
id: this.id,
|
||||
nickname: options.nickname
|
||||
}
|
||||
};
|
||||
this.options = userOptions;
|
||||
this.channelPipe.addUser(this);
|
||||
};
|
||||
|
||||
/* FIXME: watch out and check in wich direction game and control commands flow */
|
||||
User.prototype.onGameCommand = function(options) {
|
||||
// repacking for transport via pipe
|
||||
var message = ProtocolHelper.encodeCommand("gameCommand", options);
|
||||
this.channelPipe.sendToUser(this.id, message);
|
||||
};
|
||||
|
|
@ -100,6 +157,31 @@ function (Parent, ProtocolHelper, nc) {
|
|||
nc.trigger(nc.ns.server.events.controlCommand.user + this.id, message);
|
||||
};
|
||||
|
||||
return User;
|
||||
// WebRTC signaling messages from the client (arriving over Socket.IO).
|
||||
|
||||
});
|
||||
User.prototype.onWebrtcOffer = function (options) {
|
||||
if (!this.webrtc || !options) return;
|
||||
this.webrtc.handleRemoteDescription(options.sdp, options.type || "offer");
|
||||
};
|
||||
|
||||
User.prototype.onWebrtcIce = function (options) {
|
||||
if (!this.webrtc || !options) return;
|
||||
this.webrtc.handleRemoteCandidate(options.candidate, options.mid);
|
||||
};
|
||||
|
||||
// Periodic stats report from client.
|
||||
User.prototype.onStats = function (options) {
|
||||
if (!options) return;
|
||||
StatsLogger.log({
|
||||
type: "stats",
|
||||
session: this.id,
|
||||
transport: options.transport || "socketio",
|
||||
rttSamples: options.rttSamples || [],
|
||||
recvRate: options.recvRate || 0,
|
||||
sendRate: options.sendRate || 0,
|
||||
gaps: options.gaps || 0
|
||||
});
|
||||
};
|
||||
|
||||
return User;
|
||||
});
|
||||
|
|
|
|||
112
app/Server/WebRTCTransport.js
Normal file
112
app/Server/WebRTCTransport.js
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
define([
|
||||
"node-datachannel"
|
||||
],
|
||||
|
||||
function (nodeDataChannel) {
|
||||
|
||||
"use strict";
|
||||
|
||||
// Per-user WebRTC peer + unreliable data channel.
|
||||
// The User class relays SDP/ICE between this transport and the client
|
||||
// (over Socket.IO). This class only deals with the WebRTC peer state.
|
||||
function WebRTCTransport(userId, callbacks) {
|
||||
this.userId = userId;
|
||||
this.callbacks = callbacks || {};
|
||||
this.unreliable = null;
|
||||
this.ready = false;
|
||||
this.createdAt = Date.now();
|
||||
|
||||
var iceServers = ["stun:stun.l.google.com:19302"];
|
||||
this.pc = new nodeDataChannel.PeerConnection("peer-" + userId, {
|
||||
iceServers: iceServers
|
||||
});
|
||||
|
||||
var self = this;
|
||||
|
||||
this.pc.onLocalDescription(function (sdp, type) {
|
||||
if (self.callbacks.onLocalDescription) {
|
||||
self.callbacks.onLocalDescription({ sdp: sdp, type: type });
|
||||
}
|
||||
});
|
||||
|
||||
this.pc.onLocalCandidate(function (candidate, mid) {
|
||||
if (self.callbacks.onLocalCandidate) {
|
||||
self.callbacks.onLocalCandidate({ candidate: candidate, mid: mid });
|
||||
}
|
||||
});
|
||||
|
||||
this.pc.onDataChannel(function (dc) {
|
||||
var label = dc.getLabel();
|
||||
if (label !== "unreliable") return;
|
||||
self.unreliable = dc;
|
||||
dc.onOpen(function () {
|
||||
self.ready = true;
|
||||
if (self.callbacks.onReady) {
|
||||
self.callbacks.onReady(Date.now() - self.createdAt);
|
||||
}
|
||||
});
|
||||
dc.onMessage(function (msg) {
|
||||
if (self.callbacks.onMessage) self.callbacks.onMessage(msg);
|
||||
});
|
||||
dc.onClosed(function () {
|
||||
self.ready = false;
|
||||
if (self.callbacks.onClosed) self.callbacks.onClosed();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
WebRTCTransport.prototype.handleRemoteDescription = function (sdp, type) {
|
||||
try {
|
||||
console.log("[webrtc] " + this.userId + " setRemoteDescription " + type);
|
||||
this.pc.setRemoteDescription(sdp, type);
|
||||
this.haveRemoteDesc = true;
|
||||
// Flush any candidates that arrived before the offer.
|
||||
if (this.pendingCandidates && this.pendingCandidates.length) {
|
||||
var pending = this.pendingCandidates;
|
||||
this.pendingCandidates = [];
|
||||
for (var i = 0; i < pending.length; i++) {
|
||||
this.handleRemoteCandidate(pending[i].candidate, pending[i].mid);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("[webrtc] setRemoteDescription failed:", e && (e.message || e));
|
||||
}
|
||||
};
|
||||
|
||||
WebRTCTransport.prototype.handleRemoteCandidate = function (candidate, mid) {
|
||||
// Buffer until we have the remote description — adding candidates before
|
||||
// setRemoteDescription throws.
|
||||
if (!this.haveRemoteDesc) {
|
||||
this.pendingCandidates = this.pendingCandidates || [];
|
||||
this.pendingCandidates.push({ candidate: candidate, mid: mid });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
this.pc.addRemoteCandidate(candidate, mid || "0");
|
||||
} catch (e) {
|
||||
console.error("[webrtc] addRemoteCandidate failed:", e && (e.message || e));
|
||||
}
|
||||
};
|
||||
|
||||
WebRTCTransport.prototype.isReady = function () {
|
||||
return this.ready && this.unreliable && this.unreliable.isOpen();
|
||||
};
|
||||
|
||||
WebRTCTransport.prototype.send = function (message) {
|
||||
if (!this.isReady()) return false;
|
||||
try {
|
||||
this.unreliable.sendMessage(message);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
WebRTCTransport.prototype.destroy = function () {
|
||||
try { if (this.unreliable) this.unreliable.close(); } catch (_) {}
|
||||
try { this.pc.close(); } catch (_) {}
|
||||
this.ready = false;
|
||||
};
|
||||
|
||||
return WebRTCTransport;
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue