slight improvements to the controller

This commit is contained in:
2026-06-30 13:00:09 +01:00
parent 12b25e8821
commit 7c63796358
2 changed files with 93 additions and 17 deletions
+14 -2
View File
@@ -22,7 +22,7 @@ const CF_APP_ID = process.env.CF_APP_ID || '';
const CF_APP_TOKEN = process.env.CF_APP_TOKEN || '';
const CF_BASE = `https://rtc.live.cloudflare.com/v1/apps/${CF_APP_ID}`;
const CONTAINER = process.env.CONTAINER_NAME || 'css-test';
const IDLE_TIMEOUT = 10000; // 10 seconds
const IDLE_TIMEOUT = 60000; // 60 seconds
const NAME_SALT = process.env.NAME_SALT || 'change-this-secret';
// How long to wait after relaunching CSS before granting control
@@ -77,8 +77,10 @@ const controlQueue = [];
let containerRestarting = false;
function broadcastQueueState() {
const queueNames = controlQueue.map(e => e.visitorName);
const controllerName = currentController ? currentController.visitorName : null;
const queueNames = controlQueue
.map(e => e.visitorName)
.filter(name => name !== controllerName);
const msg = JSON.stringify({
type: 'queue_state',
controller: controllerName,
@@ -99,6 +101,12 @@ function resetIdleTimer(entry) {
}
async function applyController(entry) {
// Defensive: if this exact entry is sitting in the queue (e.g. a race
// between request_control calls), pull it out so it's never displayed
// as both controller and queued.
const qIdx = controlQueue.indexOf(entry);
if (qIdx !== -1) controlQueue.splice(qIdx, 1);
containerRestarting = true;
broadcastQueueState();
@@ -287,6 +295,10 @@ wss.on('connection', ws => {
if (!currentController) {
applyController(entry);
} else if (currentController.visitorName === visitorName) {
// Same identity already controlling (e.g. duplicate tab) — don't queue
// behind yourself.
return;
} else {
controlQueue.push(entry);
console.log(`[sig] ${visitorName} joined queue position ${controlQueue.length}`);