mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-11-14 17:35:35 +01:00
Merge branch 'feature/ws-included' into 'master'
Using socket.io for websocket handling and make reforis configurable See merge request turris/reforis/foris-js!201
This commit is contained in:
commit
1ed0e7a872
13241
package-lock.json
generated
13241
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -14,6 +14,7 @@
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"main": "./src/index.js",
|
"main": "./src/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"socket.io-client": "4.4.1",
|
||||||
"axios": "^0.21.1",
|
"axios": "^0.21.1",
|
||||||
"immutability-helper": "3.0.1",
|
"immutability-helper": "3.0.1",
|
||||||
"moment": "^2.24.0",
|
"moment": "^2.24.0",
|
||||||
|
|
|
@ -25,7 +25,7 @@ export function ResetWiFiSettings({ ws, endpoint }) {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const module = "wifi";
|
const module = "wifi";
|
||||||
ws.subscribe(module).bind(module, "reset", () => {
|
ws.bind(module, "reset", () => {
|
||||||
// eslint-disable-next-line no-restricted-globals
|
// eslint-disable-next-line no-restricted-globals
|
||||||
setTimeout(() => location.reload(), 1000);
|
setTimeout(() => location.reload(), 1000);
|
||||||
});
|
});
|
||||||
|
|
|
@ -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.
|
* This is free software, licensed under the GNU General Public License v3.
|
||||||
* See /LICENSE for more information.
|
* 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 REFORIS_API_URL_PREFIX = `${REFORIS_URL_PREFIX}/api`;
|
||||||
|
|
||||||
export const ForisURLs = {
|
export const ForisURLs = {
|
||||||
|
|
|
@ -7,49 +7,33 @@
|
||||||
|
|
||||||
/* eslint no-console: "off" */
|
/* eslint no-console: "off" */
|
||||||
|
|
||||||
const PROTOCOL = window.location.protocol === "http:" ? "ws" : "wss";
|
import { REFORIS_URL_PREFIX } from "../utils/forisUrls";
|
||||||
|
|
||||||
const URL = process.env.LIGHTTPD
|
const { io } = require("socket.io-client");
|
||||||
? `${PROTOCOL}://${window.location.host}/${
|
|
||||||
process.env.WSPATH || "foris-ws"
|
|
||||||
}`
|
|
||||||
: `${PROTOCOL}://${window.location.hostname}:9081`;
|
|
||||||
|
|
||||||
const WAITING_FOR_CONNECTION_TIMEOUT = 500;
|
|
||||||
|
|
||||||
export class WebSockets {
|
export class WebSockets {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.ws = new WebSocket(URL);
|
this.socket = io("/notifications", {
|
||||||
this.ws.onerror = (e) => {
|
path: `${REFORIS_URL_PREFIX}/reforis-ws`,
|
||||||
console.error("WS: Error:", e);
|
});
|
||||||
};
|
this.connection = null;
|
||||||
this.ws.onmessage = (e) => {
|
this.socket.on("disconnect", (reason) => {
|
||||||
console.debug(`WS: Received Message: ${e.data}`);
|
this.connection = null;
|
||||||
const data = JSON.parse(e.data);
|
console.debug(`SocketIO disconnected (${reason})`);
|
||||||
this.dispatch(data);
|
});
|
||||||
};
|
this.socket.on("notification", (message) => {
|
||||||
this.ws.onopen = () => {
|
console.debug("WS: Received Message:", message);
|
||||||
console.debug("WS: Connection open.");
|
this.dispatch(message);
|
||||||
};
|
});
|
||||||
this.ws.onclose = () => {
|
this.socket.on("connect", (connection) => {
|
||||||
console.debug("WS: Connection closed.");
|
this.connection = connection;
|
||||||
};
|
console.debug(`SocketIO connected.`);
|
||||||
|
});
|
||||||
|
|
||||||
// callbacks[module][action]
|
// callbacks[module][action]
|
||||||
this.callbacks = {};
|
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) {
|
bind(module, action, callback) {
|
||||||
this.callbacks[module] = this.callbacks[module] || {};
|
this.callbacks[module] = this.callbacks[module] || {};
|
||||||
this.callbacks[module][action] = this.callbacks[module][action] || [];
|
this.callbacks[module][action] = this.callbacks[module][action] || [];
|
||||||
|
@ -57,13 +41,6 @@ export class WebSockets {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(module) {
|
|
||||||
this.waitForConnection(() => {
|
|
||||||
this.send("subscribe", module);
|
|
||||||
});
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
unbind(module, action, callback) {
|
unbind(module, action, callback) {
|
||||||
const callbacks = this.callbacks[module][action];
|
const callbacks = this.callbacks[module][action];
|
||||||
|
|
||||||
|
@ -77,25 +54,9 @@ export class WebSockets {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Object.keys(this.callbacks[module]).length === 0) {
|
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];
|
delete this.callbacks[module];
|
||||||
});
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send(action, params) {
|
|
||||||
const payload = JSON.stringify({ action, params });
|
|
||||||
this.waitForConnection(() => {
|
|
||||||
this.ws.send(payload);
|
|
||||||
});
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,18 +68,15 @@ export class WebSockets {
|
||||||
chain = this.callbacks[json.module][json.action];
|
chain = this.callbacks[json.module][json.action];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof TypeError) {
|
if (error instanceof TypeError) {
|
||||||
console.warn(
|
console.debug(
|
||||||
`Callback for this message wasn't found:${error.data}`
|
`Callbacks for this module wasn't found: ${json.module}`
|
||||||
);
|
);
|
||||||
} else throw error;
|
} else throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof chain === "undefined") return;
|
if (typeof chain === "undefined") return;
|
||||||
|
|
||||||
|
console.debug("Handling WS message", json);
|
||||||
chain.forEach((callback) => callback(json));
|
chain.forEach((callback) => callback(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
|
||||||
this.ws.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ export function useWSForisModule(
|
||||||
setData(message.data);
|
setData(message.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.subscribe(module).bind(module, action, callback);
|
ws.bind(module, action, callback);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
ws.unbind(module, action, callback);
|
ws.unbind(module, action, callback);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user