// Auth via Sec-WebSocket-Protocol: ['vibex.auth', token] keeps the token
// out of the URL and out of access logs. The server validates it and
// derives the identity from the token.
const socket = new WebSocket(`${wsBaseUrl}/ws/chats/${activeChatId}`, [
'vibex.auth',
token,
]);
socket.onclose = (event) => {
emitStatus('closed', { closeCode: event.code });
if (!shouldReconnectRef.current) return;
const delay = reconnectDelayMsRef.current;
reconnectDelayMsRef.current = Math.min(
Math.round(reconnectDelayMsRef.current * 1.6),
10_000,
);
reconnectTimeoutRef.current = window.setTimeout(() => connect(true), delay);
};
// The connection is back — reconnect now, do not wait for the next tick.
const handleOnline = () => {
if (!shouldReconnectRef.current) return;
reconnectDelayMsRef.current = 1500;
connect(true);
};