Skip to content

Commit

Permalink
SUKU discord webhook error handling added, websocket port change impl…
Browse files Browse the repository at this point in the history
…emented ED-76
  • Loading branch information
sukuwc committed Jul 11, 2022
1 parent 23723f5 commit 5178800
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 94 deletions.
20 changes: 16 additions & 4 deletions src/app/main/ErrorConsole.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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'){
Expand Down
7 changes: 5 additions & 2 deletions src/app/main/modals/Welcome.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@
onDestroy(()=>{
if ($attachment.element === attachmentElement){
$attachment = undefined
if ($attachment !== undefined){
if ($attachment.element === attachmentElement){
$attachment = undefined
}
}
});
Expand Down
10 changes: 9 additions & 1 deletion src/app/main/panels/WebsocketMonitor/WebsocketMonitor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -149,13 +149,21 @@
}
function portChange(){
wss_change_port($appSettings.persistant.wssPort)
}
</script>

<config-debug class="w-full h-full flex flex-col p-4 z-10 bg-primary">

<div class="text-white">
Editor v{$appSettings.version.major}.{$appSettings.version.minor}.{$appSettings.version.patch}
</div>

<input type="number" bind:value={$appSettings.persistant.wssPort} on:change={portChange}/>

<div class="text-white">
Websocket Monitor
</div>
Expand Down
9 changes: 7 additions & 2 deletions src/app/runtime/app-helper.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const appSettings = writable({
owner: {neme: undefined}
},
persistant: {
wssPort: 1337,
moduleRotation: 0,
welcomeOnStartup: true,
lastVersion: '',
Expand All @@ -109,6 +110,7 @@ export const profileListRefresh = writable(0);
export const presetListRefresh = writable(0);

let persistant = {
wssPort: 1337,
moduleRotation: 0,
welcomeOnStartup: true,
lastVersion: '',
Expand Down Expand Up @@ -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);
}

});

Expand Down
96 changes: 52 additions & 44 deletions src/app/runtime/websocket.js
Original file line number Diff line number Diff line change
@@ -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(){

Expand All @@ -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))
Expand All @@ -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)
});
108 changes: 67 additions & 41 deletions src/ipcmain_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})


Expand Down

0 comments on commit 5178800

Please sign in to comment.