Guard against duplicate websocket connections. Closes #2773

This commit is contained in:
Gabe Kangas
2023-03-31 21:00:56 -07:00
parent 091d2433df
commit 597281bbb2
2 changed files with 33 additions and 2 deletions

View File

@@ -240,6 +240,7 @@ export const ClientConfigStore: FC = () => {
const savedAccessToken = getLocalStorage(ACCESS_TOKEN_KEY); const savedAccessToken = getLocalStorage(ACCESS_TOKEN_KEY);
if (savedAccessToken) { if (savedAccessToken) {
setAccessToken(savedAccessToken); setAccessToken(savedAccessToken);
return; return;
} }
@@ -267,6 +268,7 @@ export const ClientConfigStore: FC = () => {
const resetAndReAuth = () => { const resetAndReAuth = () => {
setLocalStorage(ACCESS_TOKEN_KEY, ''); setLocalStorage(ACCESS_TOKEN_KEY, '');
setAccessToken(null); setAccessToken(null);
ws?.shutdown();
handleUserRegistration(); handleUserRegistration();
}; };
@@ -358,6 +360,12 @@ export const ClientConfigStore: FC = () => {
const startChat = async () => { const startChat = async () => {
try { try {
if (ws) {
ws?.shutdown();
setWebsocketService(null);
ws = null;
}
const { socketHostOverride } = clientConfig; const { socketHostOverride } = clientConfig;
// Get a copy of the browser location without #fragments. // Get a copy of the browser location without #fragments.
@@ -403,9 +411,23 @@ export const ClientConfigStore: FC = () => {
}, []); }, []);
useEffect(() => { useEffect(() => {
if (!clientConfig.chatDisabled && accessToken && hasLoadedConfig) { if (clientConfig.chatDisabled) {
startChat(); return;
} }
if (!accessToken) {
return;
}
if (!hasLoadedConfig) {
return;
}
if (ws) {
return;
}
startChat();
}, [hasLoadedConfig, accessToken]); }, [hasLoadedConfig, accessToken]);
useEffect(() => { useEffect(() => {

View File

@@ -43,6 +43,11 @@ export default class WebsocketService {
if (!this.host) { if (!this.host) {
return; return;
} }
if (this.isShutdown) {
return;
}
const url = new URL(this.host); const url = new URL(this.host);
url.protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; url.protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
url.pathname = '/ws'; url.pathname = '/ws';
@@ -76,6 +81,10 @@ export default class WebsocketService {
} }
scheduleReconnect() { scheduleReconnect() {
if (this.isShutdown) {
return;
}
if (this.websocketReconnectTimer) { if (this.websocketReconnectTimer) {
clearTimeout(this.websocketReconnectTimer); clearTimeout(this.websocketReconnectTimer);
} }