freechat/freechat-client/main.js
shockrah 40351f934e + New cache ipc handlers for adding server caches
Interface for this is fiddly and probably requires real docs to be further developed
without losing my mind doc the whole cache at some point
+ New cache ipc handlers for adding a new open web socket
Web socket comes with some basic listeners, however very litte/nothing
is being done check the health of these connections or to try when possible.

+ Cache now adds actual message objects to its message arrays instead of raw strings (wew)
+ Events module has been added to move the parsing logic/validation away from everything else

+ The basic Event structure has some niftier-than-you-think behavior for data acccess which the cache can leverage for more concise+ correct behavior
2021-04-14 23:00:26 -07:00

130 lines
3.2 KiB
JavaScript

const { ipcMain } = require('electron')
const { app, BrowserWindow } = require('electron')
const { ArgumentParser } = require('argparse')
const path = require('path')
const URL = require('url')
const WebSocket = require('ws')
const { Config, from_cli_parser } = require('./src/config.js')
const { Cache } = require('./src/cache')
const arguments = (() => {
const parser = new ArgumentParser({
description: 'Freechat client'
})
parser.add_argument('-c', '--config', {help: 'Specify config path'})
return parser
})()
let win
let config = from_cli_parser(arguments)
let cache = new Cache()
let wsclient = null
// NOTE: this line is only here for demonstration as the current make script exposes this var
// so that we can test against self signed certificates but still use ssl
// process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = '0'
function createWin() {
win = new BrowserWindow({
width: 800,
height: 600,
minWidth: 640,
minHeight: 480,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname + '/preload.js')
},
autoHideMenuBar: true,
})
win.loadFile('pages/index.html');
win.on('closed', () => {
win = null
});
}
app.on('ready', createWin);
// on mac just because the windows are closed doesn't mean the application is too
// ! The toolbar keeps things alive against its will
app.on('window-all-closed', () => {
if(process.platform !== 'darwin') {
app.quit()
}
});
app.on('activate', () => {
if(win === null) {
createWin();
load_config('./example-dev-config.json'); // for now
}
});
// returns the in-memory config reference
ipcMain.handle('config-request', async function(event, _arg) {
return config;
})
ipcMain.handle('config-update', async function(event, updated_config) {
config = new Config(updated_config.path, updated_config.data)
await config.write_disk()
})
ipcMain.handle('open-socket', async function(event, kwargs ) {
const server_conf = kwargs['conf']
const conn_url = kwargs['url']
const cache_target = server_conf['wsurl']
// shutdown the websocket incase there is already one running
if(wsclient != null) {
console.log("Live websocket found, closing for replacement")
wsclient.close()
wsclient = null
}
wsclient = new WebSocket(conn_url)
// Setting some listeners for the websocket
wsclient.on('open', function() {
cache.set_active(cache_target)
})
wsclient.on('close', function(code, reason) {
console.log("Websocket close event: ",code, reason)
})
wsclient.on('error', function(error) {
console.log('Websocket runtime error: ', error)
})
wsclient.on('message', function(message) {
try {
cache.new_message(cache_target, message)
} catch (err) {
console.log(err)
}
})
})
ipcMain.handle('cache-new-server', function(event, kwargs) {
const serv_config = kwargs['cfg']
const channels = kwargs['channels']
const messages = kwargs['messages']
// optional flag which tells the cache to set this as the active server
// NOTE: Must also be the wsurl
const active_target = kwargs['active_target']
cache.add_server(active_target, serv_config, channels, messages)
if(active_target) {
if(active_target != serv_config['wsurl']) {
throw 'Active target must be the same as the target\'s web-socket URL'
}
cache.set_active(active_target)
}
})