mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-11-14 17:35:35 +01:00
fixup! WIP: Using socket.io for websocket handling
This commit is contained in:
parent
1fae301499
commit
f07e3f323f
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -11,12 +11,21 @@ const { io } = require("socket.io-client");
|
|||
|
||||
export class WebSockets {
|
||||
constructor() {
|
||||
this.socket = io("/notifications");
|
||||
this.socket.on("disconnect", (reason) => {});
|
||||
this.socket.on("notification", (notification) => {
|
||||
// TODO call dispatch
|
||||
this.socket = io("/notifications", {
|
||||
path: "/reforis/reforis-ws",
|
||||
});
|
||||
this.connection = null;
|
||||
this.socket.on("disconnect", (reason) => {
|
||||
this.connection = null;
|
||||
console.log(`SocketIO disconnected (${reason})`);
|
||||
});
|
||||
this.socket.on("notification", (message) => {
|
||||
this.dispatch(message);
|
||||
});
|
||||
this.socket.on("connect", (connection) => {
|
||||
this.connection = connection;
|
||||
console.log(`SocketIO connected.`);
|
||||
});
|
||||
this.socket.on("connect", (socket) => {});
|
||||
|
||||
// callbacks[module][action]
|
||||
this.callbacks = {};
|
||||
|
@ -42,27 +51,18 @@ export class WebSockets {
|
|||
}
|
||||
|
||||
if (Object.keys(this.callbacks[module]).length === 0) {
|
||||
this.unsubscribe(module);
|
||||
delete this.callbacks[module];
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
send(action, params) {
|
||||
// TODO may not be required
|
||||
const payload = JSON.stringify({ action, params });
|
||||
this.waitForConnection(() => {
|
||||
this.ws.send(payload);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
dispatch(json) {
|
||||
if (!json.module) return;
|
||||
|
||||
let chain;
|
||||
try {
|
||||
console.log(`Handling: ${json}`);
|
||||
chain = this.callbacks[json.module][json.action];
|
||||
} catch (error) {
|
||||
if (error instanceof TypeError) {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user