Implemented saveSettings websocket module

This commit is contained in:
RemixDev 2021-05-13 11:40:32 +02:00
parent a3ef23b289
commit 7b16ddc91a
34 changed files with 41 additions and 16 deletions

View File

@ -1,6 +1,12 @@
// @ts-expect-error
import deemix from 'deemix'
export const settings: any = deemix.settings.load()
export const loadSettings = deemix.settings.load
export const defaultSettings: any = deemix.settings.DEFAULTS
export const sessionDZ: any = {}
export let settings: any = loadSettings()
export let sessionDZ: any = {}
export function saveSettings(newSettings: any) {
deemix.settings.save(newSettings)
settings = newSettings
}

View File

@ -1,3 +1,4 @@
import WebSocket from 'ws'
import { Server as WsServer } from 'ws'
import { consoleError, consoleInfo } from '../helpers/errors'
import wsModules from './modules'
@ -5,10 +6,22 @@ import wsModules from './modules'
// ? Is this needed?
// ? https://github.com/websockets/ws#how-to-detect-and-close-broken-connections
export const broadcast = function(wss:WsServer, key:string, data:any) {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({key, data}))
}
})
}
export const registerWebsocket = (wss: WsServer) => {
wss.on('connection', ws => {
ws.on('message', (message)=>{
consoleInfo(`received: ${message}`)
const data = JSON.parse(message.toString())
wsModules.forEach(module => {
ws.on(module.eventName, module.cb)
if (data.key === module.eventName) module.cb(data.data, ws, wss)
})
})
})

View File

@ -1,9 +0,0 @@
import { consoleInfo } from '../../helpers/errors'
const eventName = 'message'
const cb = (message: string) => {
consoleInfo(`received: ${message}`)
}
export default { eventName, cb }

View File

@ -1,3 +1,3 @@
import connection from './connection'
import saveSettings from './saveSettings'
export default [connection]
export default [saveSettings]

View File

@ -0,0 +1,15 @@
import { Server as WsServer } from 'ws'
import { consoleInfo } from '../../helpers/errors'
import { saveSettings } from '../../main'
import { broadcast } from '../index'
const eventName = 'saveSettings'
const cb = (data: any, ws: any, wss: WsServer) => {
const {settings, spotifySettings} = data
saveSettings(settings)
consoleInfo('Settings saved')
broadcast(wss, 'updateSettings', {settings, spotifySettings})
}
export default { eventName, cb }

2
webui

@ -1 +1 @@
Subproject commit 9710a5f19f6614fefc760e1771cb67b410ee55de
Subproject commit 134c43a85589b64d44c55eca2f645bf4dbfb87bd