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(() => {
|
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);
|
||||||
});
|
});
|
||||||
|
|
|
@ -11,12 +11,21 @@ const { io } = require("socket.io-client");
|
||||||
|
|
||||||
export class WebSockets {
|
export class WebSockets {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.socket = io("/notifications");
|
this.socket = io("/notifications", {
|
||||||
this.socket.on("disconnect", (reason) => {});
|
path: "/reforis/reforis-ws",
|
||||||
this.socket.on("notification", (notification) => {
|
});
|
||||||
// TODO call dispatch
|
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]
|
// callbacks[module][action]
|
||||||
this.callbacks = {};
|
this.callbacks = {};
|
||||||
|
@ -42,27 +51,18 @@ export class WebSockets {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Object.keys(this.callbacks[module]).length === 0) {
|
if (Object.keys(this.callbacks[module]).length === 0) {
|
||||||
this.unsubscribe(module);
|
|
||||||
delete this.callbacks[module];
|
delete this.callbacks[module];
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
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) {
|
dispatch(json) {
|
||||||
if (!json.module) return;
|
if (!json.module) return;
|
||||||
|
|
||||||
let chain;
|
let chain;
|
||||||
try {
|
try {
|
||||||
|
console.log(`Handling: ${json}`);
|
||||||
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) {
|
||||||
|
|
|
@ -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