From 8be2efbe48344a2ec5001132bd2da76faac07ca9 Mon Sep 17 00:00:00 2001 From: "C. Fuhrman" Date: Wed, 30 Oct 2024 17:20:13 -0400 Subject: [PATCH] try to force the correct url (ws: or wss:) on the socket when it's created --- client/src/services/WebsocketService.tsx | 31 +++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/client/src/services/WebsocketService.tsx b/client/src/services/WebsocketService.tsx index c5f74be..1ee91fb 100644 --- a/client/src/services/WebsocketService.tsx +++ b/client/src/services/WebsocketService.tsx @@ -21,14 +21,21 @@ class WebSocketService { private socket: Socket | null = null; connect(backendUrl: string): Socket { - // console.log(backendUrl); - this.socket = io(`${backendUrl}`, { + console.log(`WebSocketService.connect(${backendUrl})`); + + // Ensure the URL uses wss: if the page is loaded over https: + const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; + const url = backendUrl.replace(/^http:/, protocol); + console.log(`WebSocketService.connect: changed url=${url}`); + + this.socket = io(url, { transports: ['websocket'], reconnectionAttempts: 1 }); + return this.socket; } - + disconnect() { if (this.socket) { @@ -74,15 +81,15 @@ class WebSocketService { // idQuestion: string ) { if (this.socket) { - this.socket?.emit('submit-answer', - // { - // answer: answer, - // roomName: roomName, - // username: username, - // idQuestion: idQuestion - // } - answerData - ); + this.socket?.emit('submit-answer', + // { + // answer: answer, + // roomName: roomName, + // username: username, + // idQuestion: idQuestion + // } + answerData + ); } } }