mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-01 02:52:49 +08:00
The gateway only publishes `connected` once the directory snapshot lands in
`sendInitialState()`. Between authenticating and that point the session is in
`syncing` while `hasConnectedOnce` is still false. `onDisconnect` required both
`hasConnectedOnce` and `state === "connected"`, so a transport drop inside that
gap returned early and did nothing:
* no `disconnected` frame for the browser,
* no `scheduleReconnect`,
* no peer/WeRTC rollback, no teardown.
The session stayed in `syncing` with the browser socket still open, the client
kept showing the connecting state forever, and nothing could recover it short
of the user closing the tab. `onKick` had the same gap via its own
`if (!hasConnectedOnce) return;` guard, so a kick or ban during the same window
was silently ignored while the gateway kept the dead session alive.
The window is not momentary. `tsClient.connect()` resolves even when the
directory reconciliation fails (the failure is logged and the explicit snapshot
emit is skipped), and `resetDirectoryForReconnect()` clears the snapshot on
every recovery, so each reconnect attempt opens the window again.
Treat the settling states (`authenticating`, `syncing`) as an explicit concept
and handle transport loss there:
* a drop during the first connect is a connect failure, not an interruption:
report `connectionFailed`, close with 4003 and tear the session down;
* the same reporting is now shared by the rejected-`connect()` path through a
single `failConnect` helper, so a transport that reports itself twice (a
`disconnected` event plus a rejected connect) still fails exactly once;
* a drop during a *reconnect* handshake returns the session to `reconnecting`
and schedules another attempt instead of stalling in `syncing`;
* a kick that lands before the session settles is reported and terminal.
A drop on an established session keeps the previous behaviour: `disconnected`
plus a scheduled reconnect.
Covered by three regression tests over the real WebSocket wiring with only the
external SDK stubbed: a drop before the directory sync, a kick in the same
window, and a drop during a reconnect handshake. All three hang on the old code.