1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-11-14 17:35:35 +01:00

Using socket.io for websocket handling and make reforis configurable

Socket.io wrapper is used to handle websockets now,
this means that websocket logic had to be redone.

Also it is necessary to set `REFORIS_PREFIX` env variable
during the build process. To set the path of backend url.
It was previously fixed to `/reforis`.
This commit is contained in:
Stepan Henek 2022-04-21 17:37:25 +02:00
parent f3a1090dbd
commit d435a514ce
No known key found for this signature in database
GPG Key ID: 08081019647EB8F2
6 changed files with 6673 additions and 6747 deletions

13325
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@
"license": "GPL-3.0",
"main": "./src/index.js",
"dependencies": {
"socket.io-client": "4.4.1",
"axios": "^0.21.1",
"immutability-helper": "3.0.1",
"moment": "^2.24.0",

View File

@ -25,7 +25,7 @@ export function ResetWiFiSettings({ ws, endpoint }) {
useEffect(() => {
const module = "wifi";
ws.subscribe(module).bind(module, "reset", () => {
ws.bind(module, "reset", () => {
// eslint-disable-next-line no-restricted-globals
setTimeout(() => location.reload(), 1000);
});

View File

@ -1,11 +1,11 @@
/*
* Copyright (C) 2019-2021 CZ.NIC z.s.p.o. (http://www.nic.cz/)
* Copyright (C) 2019-2022 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.
*/
export const REFORIS_URL_PREFIX = "/reforis";
export const REFORIS_URL_PREFIX = process.env.REFORIS_PREFIX || "";
export const REFORIS_API_URL_PREFIX = `${REFORIS_URL_PREFIX}/api`;
export const ForisURLs = {

View File

@ -7,49 +7,33 @@
/* eslint no-console: "off" */
const PROTOCOL = window.location.protocol === "http:" ? "ws" : "wss";
import { REFORIS_URL_PREFIX } from "../utils/forisUrls";
const URL = process.env.LIGHTTPD
? `${PROTOCOL}://${window.location.host}/${
process.env.WSPATH || "foris-ws"
}`
: `${PROTOCOL}://${window.location.hostname}:9081`;
const WAITING_FOR_CONNECTION_TIMEOUT = 500;
const { io } = require("socket.io-client");
export class WebSockets {
constructor() {
this.ws = new WebSocket(URL);
this.ws.onerror = (e) => {
console.error("WS: Error:", e);
};
this.ws.onmessage = (e) => {
console.debug(`WS: Received Message: ${e.data}`);
const data = JSON.parse(e.data);
this.dispatch(data);
};
this.ws.onopen = () => {
console.debug("WS: Connection open.");
};
this.ws.onclose = () => {
console.debug("WS: Connection closed.");
};
this.socket = io("/notifications", {
path: `${REFORIS_URL_PREFIX}/reforis-ws`,
});
this.connection = null;
this.socket.on("disconnect", (reason) => {
this.connection = null;
console.debug(`SocketIO disconnected (${reason})`);
});
this.socket.on("notification", (message) => {
console.debug("WS: Received Message:", message);
this.dispatch(message);
});
this.socket.on("connect", (connection) => {
this.connection = connection;
console.debug(`SocketIO connected.`);
});
// 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] || [];
@ -57,13 +41,6 @@ export class WebSockets {
return this;
}
subscribe(module) {
this.waitForConnection(() => {
this.send("subscribe", module);
});
return this;
}
unbind(module, action, callback) {
const callbacks = this.callbacks[module][action];
@ -77,28 +54,12 @@ export class WebSockets {
}
if (Object.keys(this.callbacks[module]).length === 0) {
this.unsubscribe(module);
delete this.callbacks[module];
}
return this;
}
unsubscribe(module) {
this.waitForConnection(() => {
this.send("unsubscribe", module);
delete this.callbacks[module];
});
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;
@ -107,18 +68,15 @@ export class WebSockets {
chain = this.callbacks[json.module][json.action];
} catch (error) {
if (error instanceof TypeError) {
console.warn(
`Callback for this message wasn't found:${error.data}`
console.debug(
`Callbacks for this module wasn't found: ${json.module}`
);
} else throw error;
}
if (typeof chain === "undefined") return;
console.debug("Handling WS message", json);
chain.forEach((callback) => callback(json));
}
close() {
this.ws.close();
}
}

View File

@ -32,7 +32,7 @@ export function useWSForisModule(
setData(message.data);
}
ws.subscribe(module).bind(module, action, callback);
ws.bind(module, action, callback);
return () => {
ws.unbind(module, action, callback);