2019-08-23 15:20:22 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
|
|
|
|
*
|
|
|
|
* This is free software, licensed under the GNU General Public License v3.
|
|
|
|
* See /LICENSE for more information.
|
|
|
|
*/
|
|
|
|
|
2019-08-27 16:09:18 +02:00
|
|
|
/* eslint no-console: "off" */
|
|
|
|
|
2019-12-26 23:34:59 +01:00
|
|
|
import { ForisURLs } from "../forisUrls";
|
2019-08-23 15:20:22 +02:00
|
|
|
|
|
|
|
const PROTOCOL = window.location.protocol === "http:" ? "ws" : "wss";
|
|
|
|
|
|
|
|
const URL = process.env.LIGHTTPD
|
|
|
|
? `${PROTOCOL}://${window.location.hostname}/foris-ws`
|
|
|
|
: `${PROTOCOL}://${window.location.hostname}:${9081}`;
|
|
|
|
|
|
|
|
const WAITING_FOR_CONNECTION_TIMEOUT = 500;
|
|
|
|
|
2019-08-27 17:46:45 +02:00
|
|
|
export class WebSockets {
|
2019-08-23 15:20:22 +02:00
|
|
|
constructor() {
|
|
|
|
this.ws = new WebSocket(URL);
|
|
|
|
this.ws.onerror = (e) => {
|
2019-08-27 16:09:18 +02:00
|
|
|
if (window.location.pathname !== ForisURLs.login) {
|
2019-08-23 15:20:22 +02:00
|
|
|
console.error("WS: Error observed, you aren't logged probably.");
|
2019-08-27 16:09:18 +02:00
|
|
|
window.location.replace(ForisURLs.login);
|
2019-08-23 15:20:22 +02:00
|
|
|
}
|
2019-12-17 17:45:55 +01:00
|
|
|
console.error(`WS: Error: ${e}`);
|
2019-08-23 15:20:22 +02:00
|
|
|
};
|
|
|
|
this.ws.onmessage = (e) => {
|
2019-12-17 17:45:55 +01:00
|
|
|
console.debug(`WS: Received Message: ${e.data}`);
|
2019-08-23 15:20:22 +02:00
|
|
|
const data = JSON.parse(e.data);
|
|
|
|
this.dispatch(data);
|
|
|
|
};
|
|
|
|
this.ws.onopen = () => {
|
2019-12-17 17:45:55 +01:00
|
|
|
console.debug("WS: Connection open.");
|
2019-08-23 15:20:22 +02:00
|
|
|
};
|
|
|
|
this.ws.onclose = () => {
|
2019-12-17 17:45:55 +01:00
|
|
|
console.debug("WS: Connection closed.");
|
2019-08-23 15:20:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// callbacks[module][action]
|
|
|
|
this.callbacks = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
waitForConnection(callback) {
|
|
|
|
if (this.ws.readyState === 1) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
const that = this;
|
|
|
|
setTimeout(() => {
|
|
|
|
that.waitForConnection(callback);
|
|
|
|
}, WAITING_FOR_CONNECTION_TIMEOUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bind(module, action, callback) {
|
|
|
|
this.callbacks[module] = this.callbacks[module] || {};
|
|
|
|
this.callbacks[module][action] = this.callbacks[module][action] || [];
|
|
|
|
this.callbacks[module][action].push(callback);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:05:56 +01:00
|
|
|
subscribe(module) {
|
2019-08-23 15:20:22 +02:00
|
|
|
this.waitForConnection(() => {
|
2019-12-04 17:05:56 +01:00
|
|
|
this.send("subscribe", module);
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
unbind(module, action, callback) {
|
|
|
|
const callbacks = this.callbacks[module][action];
|
|
|
|
|
|
|
|
const index = callbacks.indexOf(callback);
|
|
|
|
if (index !== -1) {
|
|
|
|
callbacks.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callbacks.length === 0) {
|
|
|
|
delete this.callbacks[module][action];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(this.callbacks[module]).length === 0) {
|
|
|
|
this.unsubscribe(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe(module) {
|
|
|
|
this.waitForConnection(() => {
|
|
|
|
this.send("unsubscribe", module);
|
|
|
|
delete this.callbacks[module];
|
2019-08-23 15:20:22 +02:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
send(action, params) {
|
|
|
|
const payload = JSON.stringify({ action, params });
|
|
|
|
this.waitForConnection(() => {
|
|
|
|
this.ws.send(payload);
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(json) {
|
|
|
|
if (!json.module) return;
|
|
|
|
|
|
|
|
let chain;
|
|
|
|
try {
|
|
|
|
chain = this.callbacks[json.module][json.action];
|
2019-12-04 17:05:56 +01:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof TypeError) {
|
2019-12-17 17:45:55 +01:00
|
|
|
console.warn(`Callback for this message wasn't found:${error.data}`);
|
2019-12-04 17:05:56 +01:00
|
|
|
} else throw error;
|
2019-08-23 15:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof chain === "undefined") return;
|
|
|
|
|
2019-12-04 17:05:56 +01:00
|
|
|
chain.forEach((callback) => callback(json));
|
2019-08-23 15:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.ws.close();
|
|
|
|
}
|
|
|
|
}
|