From 2f17734de12df0f79e69153bba5b459c12c813cf Mon Sep 17 00:00:00 2001 From: Roman Savich Date: Tue, 15 Jun 2021 18:44:43 +0300 Subject: [PATCH] test:dynamic websocket url generation --- frontend/src/store/index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index c1694714..1d2b539c 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -164,9 +164,17 @@ export default new Vuex.Store({ } } }, - + //@ts-ignore setupConnection({ commit }, { id, type, onOpen }) { - const connection = new WebSocket(`${process.env.VUE_APP_WEBSOCKET_URL}/${id}/`); + let connection; + if (process.env.VUE_APP_WEBSOCKET_URL.includes('ws:/') || process.env.VUE_APP_WEBSOCKET_URL.includes('wss:/')) { + connection = new WebSocket(`${process.env.VUE_APP_WEBSOCKET_URL}/${id}/`); + } else { + let protocol = window.location.protocol; + let socketProtocol = protocol.includes('http') ? 'ws' : 'wss'; + let socketPath = `${socketProtocol}://${window.location.host}/api/ws/${id}/`; + connection = new WebSocket(socketPath); + } connection.onmessage = (event) => { const data = JSON.parse(event.data);