Make it choose ws/wss protocol based on URL's protocol. (#196)

When you open the app with https: protocol it will chose wss secured socket connection instead of hardcoded addresses / domains.
This commit is contained in:
sergystepanov 2020-06-13 09:23:01 +03:00 committed by GitHub
parent 42d7e166e8
commit 27f8fdc117
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,15 +30,10 @@ const socket = (() => {
let curPacketId = '';
const init = (roomId, zone) => {
const paramString = new URLSearchParams({room_id: roomId, zone: zone})
// if localhost, local LAN connection
if (location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.hostname.startsWith("192.168")) {
scheme = "ws"
} else {
scheme = "wss"
}
conn = new WebSocket(`${scheme}://${location.host}/ws?${paramString.toString()}`);
const params = new URLSearchParams({room_id: roomId, zone: zone}).toString()
const address = `${location.protocol !== 'https:' ? 'ws' : 'wss'}://${location.host}/ws?${params}`;
console.info(`[ws] connecting to ${address}`);
conn = new WebSocket(address);
// Clear old roomID
conn.onopen = () => {