From e75b20d6ca805497de0df7b1026259c354c7d918 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 18 Oct 2022 20:40:57 -0700 Subject: [PATCH] Support customSocketOverride value for websocket. Closes #2225 --- web/components/stores/ClientConfigStore.tsx | 17 ++++------------- web/interfaces/client-config.model.ts | 1 + web/services/websocket-service.ts | 18 +++++++----------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/web/components/stores/ClientConfigStore.tsx b/web/components/stores/ClientConfigStore.tsx index 45ced285f..1bd6a2c6f 100644 --- a/web/components/stores/ClientConfigStore.tsx +++ b/web/components/stores/ClientConfigStore.tsx @@ -21,7 +21,7 @@ import { MessageVisibilityEvent, SocketEvent, } from '../../interfaces/socket-events'; - +import { mergeMeta } from '../../utils/helpers'; import handleConnectedClientInfoMessage from './eventhandlers/connected-client-info-handler'; import ServerStatusService from '../../services/status-service'; import handleNameChangeEvent from './eventhandlers/handleNameChangeEvent'; @@ -148,17 +148,6 @@ export const visibleChatMessagesSelector = selector({ }, }); -// Take a nested object of state metadata and merge it into -// a single flattened node. -function mergeMeta(meta) { - return Object.keys(meta).reduce((acc, key) => { - const value = meta[key]; - Object.assign(acc, value); - - return acc; - }, {}); -} - export const ClientConfigStore: FC = () => { const [appState, appStateSend, appStateService] = useMachine(appStateModel); const [currentUser, setCurrentUser] = useRecoilState(currentUserAtom); @@ -324,7 +313,9 @@ export const ClientConfigStore: FC = () => { const startChat = async () => { try { - ws = new WebsocketService(accessToken, '/ws'); + const { socketHostOverride } = clientConfig; + const host = socketHostOverride || window.location.toString(); + ws = new WebsocketService(accessToken, '/ws', host); ws.handleMessage = handleMessage; setWebsocketService(ws); } catch (error) { diff --git a/web/interfaces/client-config.model.ts b/web/interfaces/client-config.model.ts index e34e80b67..e1971257c 100644 --- a/web/interfaces/client-config.model.ts +++ b/web/interfaces/client-config.model.ts @@ -16,6 +16,7 @@ export interface ClientConfig { federation: Federation; notifications: Notifications; authentication: Authentication; + socketHostOverride?: string; } interface Authentication { diff --git a/web/services/websocket-service.ts b/web/services/websocket-service.ts index cac5544db..d6fdca2cd 100644 --- a/web/services/websocket-service.ts +++ b/web/services/websocket-service.ts @@ -20,24 +20,20 @@ export default class WebsocketService { handleMessage?: (message: SocketEvent) => void; - constructor(accessToken, path) { + constructor(accessToken, path, host) { this.accessToken = accessToken; this.path = path; this.websocketReconnectTimer = null; - - // this.send = this.send.bind(this); - this.createAndConnect = this.createAndConnect.bind(this); - // this.scheduleReconnect = this.scheduleReconnect.bind(this); - // this.onError = this.onError.bind(this); - this.shutdown = this.shutdown.bind(this); - this.isShutdown = false; - this.createAndConnect(); + this.createAndConnect = this.createAndConnect.bind(this); + this.shutdown = this.shutdown.bind(this); + + this.createAndConnect(host); } - createAndConnect() { - const url = new URL(window.location.toString()); + createAndConnect(host) { + const url = new URL(host); url.protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; url.pathname = '/ws'; url.port = window.location.port === '3000' ? '8080' : window.location.port;