mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 01:21:20 -04:00
dead socket kicking and connection info pill
This commit is contained in:
@@ -14,11 +14,14 @@ function registerVerificationHooks(deps) {
|
||||
} = deps;
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
socket.data = socket.data || {};
|
||||
socket.data.connectedAt = Date.now();
|
||||
identifySocket(socket, {});
|
||||
|
||||
socket.on('session:identify', (payload = {}, cb = () => {}) => {
|
||||
try {
|
||||
const result = identifySocket(socket, payload || {});
|
||||
socket.data.lastClientIdentifyAt = Date.now();
|
||||
cb({ success: true, ...result });
|
||||
} catch (err) {
|
||||
cb({ error: err.message });
|
||||
|
||||
@@ -28,6 +28,8 @@ const { createRequestFlow } = require('./requestFlow');
|
||||
const { registerVerificationHooks } = require('./hooks');
|
||||
|
||||
const verificationEvents = new EventEmitter();
|
||||
const IDENTITY_TIMEOUT_MS = 2 * 60 * 1000;
|
||||
const IDENTITY_SWEEP_INTERVAL_MS = 15 * 1000;
|
||||
|
||||
function emitChange(reason, payload = {}) {
|
||||
verificationEvents.emit('change', { reason, ...payload });
|
||||
@@ -145,6 +147,27 @@ registerVerificationHooks({
|
||||
emitChange,
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
io.sockets.sockets.forEach((socket) => {
|
||||
if (!socket?.id) return;
|
||||
const role = getRole(socket);
|
||||
if (role === 'admin' || role === 'lockdown') return;
|
||||
const connectedAt = Number(socket?.data?.connectedAt || 0);
|
||||
const lastClientIdentifyAt = Number(socket?.data?.lastClientIdentifyAt || 0);
|
||||
const referenceTs = lastClientIdentifyAt || connectedAt;
|
||||
if (!referenceTs) return;
|
||||
if (now - referenceTs < IDENTITY_TIMEOUT_MS) return;
|
||||
logger.info('Disconnecting socket due to stale identity heartbeat', {
|
||||
socketId: socket.id,
|
||||
role,
|
||||
ageMs: now - referenceTs,
|
||||
hadClientIdentify: Boolean(lastClientIdentifyAt),
|
||||
});
|
||||
socket.disconnect(true);
|
||||
});
|
||||
}, IDENTITY_SWEEP_INTERVAL_MS);
|
||||
|
||||
module.exports = {
|
||||
identifySocket,
|
||||
getVerificationStatus: verificationFlow.getVerificationStatus,
|
||||
|
||||
Reference in New Issue
Block a user