try to force the correct url (ws: or wss:) on the socket when it's created

This commit is contained in:
C. Fuhrman 2024-10-30 17:20:13 -04:00
parent 46dae02d47
commit 8be2efbe48

View file

@ -21,11 +21,18 @@ class WebSocketService {
private socket: Socket | null = null; private socket: Socket | null = null;
connect(backendUrl: string): Socket { connect(backendUrl: string): Socket {
// console.log(backendUrl); console.log(`WebSocketService.connect(${backendUrl})`);
this.socket = io(`${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'], transports: ['websocket'],
reconnectionAttempts: 1 reconnectionAttempts: 1
}); });
return this.socket; return this.socket;
} }
@ -75,14 +82,14 @@ class WebSocketService {
) { ) {
if (this.socket) { if (this.socket) {
this.socket?.emit('submit-answer', this.socket?.emit('submit-answer',
// { // {
// answer: answer, // answer: answer,
// roomName: roomName, // roomName: roomName,
// username: username, // username: username,
// idQuestion: idQuestion // idQuestion: idQuestion
// } // }
answerData answerData
); );
} }
} }
} }