frontend/smart-hut/src/endpoint.js

20 lines
600 B
JavaScript
Raw Normal View History

2020-04-12 15:46:16 +00:00
/**
* Returns the endpoint URL (SmartHut backend URL)
* @returns {String} endpoint URL
*/
export function endpointURL() {
2020-05-12 13:18:33 +00:00
return window.BACKEND_URL !== '__BACKEND_URL__'
2020-04-12 15:46:16 +00:00
? window.BACKEND_URL
2020-05-12 13:18:33 +00:00
: 'http://localhost:8080';
2020-04-12 15:46:16 +00:00
}
export function socketURL(token) {
const httpURL = new URL(endpointURL());
2020-05-12 13:18:33 +00:00
const isSecure = httpURL.protocol === 'https:';
const protocol = isSecure ? 'wss:' : 'ws:';
2020-04-12 15:46:16 +00:00
const port = httpURL.port || (isSecure ? 443 : 80);
const url = `${protocol}//${httpURL.hostname}:${port}/sensor-socket?token=${token}`;
2020-05-12 13:18:33 +00:00
console.log('socket url: ', url);
2020-04-12 15:46:16 +00:00
return url;
}