From 517880086525e4cf8620ee10a0541dfe397f222b Mon Sep 17 00:00:00 2001 From: sukuwc Date: Mon, 11 Jul 2022 15:07:27 +0200 Subject: [PATCH] SUKU discord webhook error handling added, websocket port change implemented ED-76 --- src/app/main/ErrorConsole.svelte | 20 +++- src/app/main/modals/Welcome.svelte | 7 +- .../WebsocketMonitor/WebsocketMonitor.svelte | 10 +- src/app/runtime/app-helper.store.js | 9 +- src/app/runtime/websocket.js | 96 +++++++++------- src/ipcmain_websocket.js | 108 +++++++++++------- 6 files changed, 156 insertions(+), 94 deletions(-) diff --git a/src/app/main/ErrorConsole.svelte b/src/app/main/ErrorConsole.svelte index 23ecb68de..c330c4562 100644 --- a/src/app/main/ErrorConsole.svelte +++ b/src/app/main/ErrorConsole.svelte @@ -28,8 +28,14 @@ analytics.track_event("application", "error console", "error notification", "error event") - const webhook = new Webhook(process.env.DISCORD_FEEDBACK_WEBHOOK); - webhook.send(`######\nError Notification\n######\n${errorMsg} `) + try { + + const webhook = new Webhook(process.env.DISCORD_FEEDBACK_WEBHOOK); + webhook.send(`######\nError Notification\n######\n${errorMsg} `) + + } catch (error) { + + } return false; } @@ -41,8 +47,14 @@ analytics.track_event("application", "error console", "error notification", "error event") - const webhook = new Webhook(process.env.DISCORD_FEEDBACK_WEBHOOK); - webhook.send(`######\nError Notification\n######\n${e.reason} `) + try { + + const webhook = new Webhook(process.env.DISCORD_FEEDBACK_WEBHOOK); + webhook.send(`######\nError Notification\n######\n${e.reason} `) + + } catch (error) { + + } } if(process.platform == 'darwin'){ diff --git a/src/app/main/modals/Welcome.svelte b/src/app/main/modals/Welcome.svelte index 2decd1426..1b3b13351 100644 --- a/src/app/main/modals/Welcome.svelte +++ b/src/app/main/modals/Welcome.svelte @@ -46,10 +46,13 @@ onDestroy(()=>{ - if ($attachment.element === attachmentElement){ - $attachment = undefined + if ($attachment !== undefined){ + if ($attachment.element === attachmentElement){ + $attachment = undefined + } } + }); diff --git a/src/app/main/panels/WebsocketMonitor/WebsocketMonitor.svelte b/src/app/main/panels/WebsocketMonitor/WebsocketMonitor.svelte index efa5eec8f..b87882f2c 100644 --- a/src/app/main/panels/WebsocketMonitor/WebsocketMonitor.svelte +++ b/src/app/main/panels/WebsocketMonitor/WebsocketMonitor.svelte @@ -4,7 +4,7 @@ import _utils, { luaParser } from "../../../runtime/_utils"; import { appSettings } from '../../../runtime/app-helper.store'; import {luadebug_store} from "../../../runtime/runtime.store" - import { wss_send_message } from '../../../runtime/websocket'; + import { wss_send_message, wss_change_port } from '../../../runtime/websocket'; let runtimeScript = ''; @@ -149,6 +149,11 @@ } + function portChange(){ + + wss_change_port($appSettings.persistant.wssPort) + } + @@ -156,6 +161,9 @@
Editor v{$appSettings.version.major}.{$appSettings.version.minor}.{$appSettings.version.patch}
+ + +
Websocket Monitor
diff --git a/src/app/runtime/app-helper.store.js b/src/app/runtime/app-helper.store.js index 800793359..2ecb86287 100644 --- a/src/app/runtime/app-helper.store.js +++ b/src/app/runtime/app-helper.store.js @@ -85,6 +85,7 @@ export const appSettings = writable({ owner: {neme: undefined} }, persistant: { + wssPort: 1337, moduleRotation: 0, welcomeOnStartup: true, lastVersion: '', @@ -109,6 +110,7 @@ export const profileListRefresh = writable(0); export const presetListRefresh = writable(0); let persistant = { + wssPort: 1337, moduleRotation: 0, welcomeOnStartup: true, lastVersion: '', @@ -198,8 +200,11 @@ function init_appsettings(){ value = 1000; } - s.persistant[key] = value; - //console.log("init", key, value); + if (value !== undefined){ + + s.persistant[key] = value; + //console.log("init", key, value); + } }); diff --git a/src/app/runtime/websocket.js b/src/app/runtime/websocket.js index d3a4f3cf7..957cdd510 100644 --- a/src/app/runtime/websocket.js +++ b/src/app/runtime/websocket.js @@ -1,7 +1,13 @@ +import { get } from "svelte/store"; import {debug_lowlevel_store} from "../main/panels/WebsocketMonitor/WebsocketMonitor.store" +import { appSettings } from "./app-helper.store"; const { ipcRenderer, app } = require('electron'); +let client +let heartbeatTimeout; + +console.log("WSS PORT", get(appSettings).persistant.wssPort) export function initialize_ws(){ @@ -17,6 +23,23 @@ export function wss_send_message(msg){ } +let wssPort = undefined; + +appSettings.subscribe(appS => { + + if (appS.persistant.wssPort !== wssPort){ + wssPort = appS.persistant.wssPort + console.log("WSS UPDATED", wssPort); + + wss_change_port(wssPort) + + } + + + + +}); + ipcRenderer.on('wss_rx', (event, args) => { debug_lowlevel_store.push_inbound(new TextEncoder().encode(args)) @@ -29,68 +52,53 @@ ipcRenderer.on('wss_tx', (event, args) => { }) -let heartbeatTimeout; -const client = new WebSocket('ws://localhost:1337'); -console.log("WS: ", client); +function websocketStart(){ -client.addEventListener('open', e => { + client = new WebSocket('ws://localhost:'+get(appSettings).persistant.wssPort); - heartbeatTimeout = setTimeout(e=>{client.close()}, 5000 + 1000); + console.log("WS: ", client); - client.addEventListener('message', function message(data) { + client.addEventListener('open', e => { - const decoded = JSON.parse(data.data) + heartbeatTimeout = setTimeout(e=>{client.close()}, 5000 + 1000); + client.addEventListener('message', function message(data) { - if (decoded.event === "grid_ping"){ + const decoded = JSON.parse(data.data) - //console.log("PING") - clearTimeout(heartbeatTimeout) - heartbeatTimeout = setTimeout(e=>{client.close()}, 5000 + 1000); + //console.log("MESSAGE", decoded) - client.send(JSON.stringify({"event":"grid_pong"})) - } - }); -}); - - -client.addEventListener('close', function clear() { - console.log("CONNECTION CLOSED") - clearTimeout(heartbeatTimeout) -}); - - - -let heartbeatTimeout2; + if (decoded.event === "grid_ping"){ -const client2 = new WebSocket('ws://localhost:1337'); + //console.log("PING") + clearTimeout(heartbeatTimeout) + heartbeatTimeout = setTimeout(e=>{client.close()}, 5000 + 1000); -console.log("WS: ", client2); - -client2.addEventListener('open', e => { + client.send(JSON.stringify({"event":"grid_pong"})) + } + }); + }); - heartbeatTimeout2 = setTimeout(e=>{client2.close()}, 5000 + 1000); - client2.addEventListener('message', function message(data) { + client.addEventListener('close', function clear() { + console.log("CONNECTION CLOSED") + clearTimeout(heartbeatTimeout) + }); - const decoded = JSON.parse(data.data) +} +export function wss_change_port(port){ + console.log("PORTCHANGE", port) + ipcRenderer.send("websocket_changePort", port) - if (decoded.event === "grid_ping"){ + if (client !== undefined){ - //console.log("PING") - clearTimeout(heartbeatTimeout2) - heartbeatTimeout2 = setTimeout(e=>{client2.close()}, 5000 + 1000); + client.close(); + } - client2.send(JSON.stringify({"event":"grid_pong"})) - } - }); -}); + websocketStart(); +} -client2.addEventListener('close', function clear() { - console.log("CONNECTION CLOSED") - clearTimeout(heartbeatTimeout2) -}); \ No newline at end of file diff --git a/src/ipcmain_websocket.js b/src/ipcmain_websocket.js index eb07a40df..db1eb01c2 100644 --- a/src/ipcmain_websocket.js +++ b/src/ipcmain_websocket.js @@ -7,79 +7,105 @@ let websocket = { }; +let wss; +let interval; -let wss = new WebSocket.Server({port: 1337}); +function startWebsocketServer(port){ -console.log("WEBSOCKET") + wss = new WebSocket.Server({port: port}); -wss.on("error", error => console.log("The server encountered an error!", error)); + wss.on("error", error => console.log("The server encountered an error!", error)); -ipcMain.on('websocket_tx', (event, arg) => { + wss.on('connection', function (ws) { - console.log(arg) - const decoded = JSON.parse(arg) + ws.isAlive = true; - const data = JSON.stringify({"event":"message", "data": decoded}) - websocket.mainWindow.webContents.send('wss_tx', data); + console.warn('WS Client connected!') - wss.clients.forEach(function each(ws) { - ws.send(data) - }); + connection = ws; -}) + ws.on('message', function message(data) { -function heartbeat(ws) { - ws.isAlive = true; -} -wss.on('connection', function (ws) { + websocket.mainWindow.webContents.send('wss_rx', data); - ws.isAlive = true; + const decoded = JSON.parse(data) + if (decoded.event === "grid_pong"){ + //console.log("WS PONG") + ws.isAlive = true; + } - console.warn('WS Client connected!') + }); - connection = ws; + ws.on('close', function(){ + console.warn('WS Client disconnected!') + + }) - ws.on('message', function message(data) { + }); - websocket.mainWindow.webContents.send('wss_rx', data); + wss.on('close', function close(){ + console.log("CLEAR INTERVAL") + clearInterval(interval); + }) - const decoded = JSON.parse(data) - if (decoded.event === "grid_pong"){ - //console.log("WS PONG") - heartbeat(ws) - } + console.log("SET INTERVAL") + interval = setInterval(function ping() { - }); + const data = JSON.stringify({"event":"grid_ping"}) + websocket.mainWindow.webContents.send('wss_tx', data); + + //console.log("PING") - ws.on('close', function(){ - console.warn('WS Client disconnected!') - clearInterval(ws.pingInterval); + wss.clients.forEach(function each(ws) { + if (ws.isAlive === false) return ws.terminate(); - }) + ws.isAlive = false; + ws.send(data) + }); + }, 5000); -}); +} + + -// close connections that have timed out -const interval = setInterval(function ping() { - const data = JSON.stringify({"event":"grid_ping"}) + +console.log("WEBSOCKET") + + +ipcMain.on('websocket_tx', (event, arg) => { + + console.log(arg) + const decoded = JSON.parse(arg) + + const data = JSON.stringify({"event":"message", "data": decoded}) websocket.mainWindow.webContents.send('wss_tx', data); wss.clients.forEach(function each(ws) { - if (ws.isAlive === false) return ws.terminate(); - - ws.isAlive = false; ws.send(data) }); -}, 5000); +}) + +ipcMain.on('websocket_changePort', (event, arg) => { + + console.log("NEW PORT", arg) + + if (wss !== undefined){ + wss.clients.forEach(function each(ws) { + ws.terminate() + }); + + wss.close(); + } + + + startWebsocketServer(arg); -wss.on('close', function close(){ - clearInterval(interval); })