add identity to socket auth hopefully will fix a lot of probem

This commit is contained in:
legop3
2026-08-03 01:09:21 -04:00
parent 15a60a58e6
commit 208b89fd7f
11 changed files with 250 additions and 240 deletions
+41
View File
@@ -1,6 +1,7 @@
// Socket Library Helper
// Purpose: Builds and exports the browser Socket.IO client with shared defaults. Scope: Centralizes connection URL/options so all modules use consistent socket behavior.
import { io } from 'socket.io-client';
import { getBrowserFingerprintId } from './browserFingerprint.js';
import { loadSettings } from '../settings/persistence.js';
const configured = import.meta.env.VITE_ROVERD_URL?.trim();
@@ -9,8 +10,48 @@ console.info('[socket] connecting to', resolvedUrl);
const settings = loadSettings();
const transportPref = settings?.page?.connectionTransport || 'websocket';
const transports = transportPref === 'polling' ? ['polling'] : ['websocket', 'polling'];
function getIdentitySurface() {
/*
Duplicate-driver protection is connection-specific, so the route must be
known during the handshake rather than waiting for a React route component
to mount. The main driver and dedicated PTZ controller are the two active
control surfaces; every other route is passive identity-wise.
*/
return window.location.pathname === '/' || window.location.pathname === '/ptz'
? 'driver'
: 'passive';
}
async function buildSocketIdentity() {
/*
Socket.IO invokes this auth callback again for every reconnection. Reading
persistence here, instead of reusing the module-level settings snapshot,
ensures that nickname, preferences, imported settings, and a server-issued
identity key are current whenever a new socket is accepted.
*/
const currentSettings = loadSettings();
const fingerprintId = await getBrowserFingerprintId();
return {
cookieUserId: String(currentSettings?.identity?.cookieUserId || '').trim(),
fingerprintId,
nickname: String(currentSettings?.profile?.nickname || '').trim(),
overseerEnabled: Boolean(currentSettings?.overseerPreference?.enabled),
identitySurface: getIdentitySurface(),
};
}
export const socket = io(resolvedUrl, {
transports,
timeout: 15000,
/*
The callback form lets the existing asynchronous fingerprint finish before
Socket.IO sends its namespace CONNECT packet. Server connection middleware
can therefore attach the canonical identity before any feature service sees
the socket, eliminating the previous connected-but-unidentified window.
*/
auth: (callback) => {
buildSocketIdentity().then(callback);
},
});
socket.on('connect_error', (err) => console.error('connect_error', err.code, err.message, err.data));