feat: add relay to slapdash

main
Ayush Mukherjee 1 year ago
parent 957599f3a3
commit a11f5c80e4

@ -7,6 +7,7 @@ import path from 'path'
const isDevelopment = process.env.NODE_ENV !== 'production' const isDevelopment = process.env.NODE_ENV !== 'production'
import { start, status, stop, state, sub, sendAll } from './utils/server' import { start, status, stop, state, sub, sendAll } from './utils/server'
import { start as relayStart, status as relayStatus, stop as relayStop, state as relayState } from './utils/relay'
// Scheme must be registered before the app is ready // Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([ protocol.registerSchemesAsPrivileged([
@ -96,6 +97,10 @@ ipcMain.handle('status', async () => {
return status() return status()
}) })
ipcMain.handle('relayStatus', async () => {
return relayStatus()
})
ipcMain.handle('server', async (e, arg) => { ipcMain.handle('server', async (e, arg) => {
if (arg === true) { if (arg === true) {
start() start()
@ -104,6 +109,14 @@ ipcMain.handle('server', async (e, arg) => {
} }
}) })
ipcMain.handle('relay', async (e, arg) => {
if (arg === true) {
relayStart()
} else {
relayStop()
}
})
ipcMain.handle('massSend', async () => { ipcMain.handle('massSend', async () => {
console.log('Sending refreshed data') console.log('Sending refreshed data')
sendAll() sendAll()

@ -24,6 +24,7 @@ const state = reactive({
brackets: [], brackets: [],
overlayStream: null, overlayStream: null,
server: false, server: false,
relay: false,
}) })
let interval = 0 let interval = 0

@ -0,0 +1,108 @@
import ws from 'ws'
import { v4 } from 'uuid'
let relay = null
let rl = null
const connections = {}
export const sub = []
let overlayStream = null
export const state = (data) => {
overlayStream = data
}
export const start = () => {
console.warn('RELAY SERVER STARTED.')
relay = new ws.Server({
port: 49322,
})
relay.on('connection', handle)
rl = new ws.WebSocket('ws://localhost:49122')
rl.onopen = () => {
console.warn('RL connected at ws://localhost:49122');
}
rl.onclose = () => {
console.warn('Lost connection to RL, not retrying');
if (relay) {
stop()
}
rl = null
}
rl.on('message', (e) => {
console.warn('Received event from RL')
Object.values(connections).forEach(v => {
const d = JSON.parse(e)
v.send(JSON.stringify(d));
})
})
}
export const stop = () => {
console.log('Received relay stop')
Object.keys(connections).forEach((k) => {
delete connections[k]
})
relay.close(() => {
relay = null
})
console.warn('RELAY SERVER CLOSED')
}
export const status = () => {
if (relay !== null)
return true
return false
}
export const sendAll = () => {
Object.values(connections).forEach(v => {
sendData(v)
})
}
const handle = (w) => {
console.warn('NEW RELAY CONNECTION')
const id = v4()
connections[id] = w
sendData(w)
w.on('message', msg => console.log(msg))
w.on('close', () => {
delete connections[id]
})
}
const onMsg = (msg, id) => {
const d = JSON.parse(msg)
if (d.hasOwnProperty('event')) {
if (d.event === 'read') {
sendData(connections[id])
} else if (d.event === 'update') {
console.warn('RECEIVED UPDATE')
sub.forEach(x => {
x(d.data)
})
}
}
}
const sendData = (w) => {
console.log('SENDING DATA')
if (overlayStream !== null) {
console.log(overlayStream)
w.send(JSON.stringify({
event: 'read',
data: overlayStream,
}))
}
}

@ -24,6 +24,10 @@
<span v-if="!store.state.server">Start</span> <span v-if="!store.state.server">Start</span>
overlay overlay
</button> </button>
<button @click.prevent="relayDo()" class="block mt-4 w-64 h-10 text-white bg-red-700 rounded-lg hover:bg-red-600">
<span v-if="store.state.relay">Stop Relay</span>
<span v-if="!store.state.relay">Start relay</span>
</button>
<!-- <button v-if="store.state.server" :disabled="streamid === ''" @click.prevent="serverDo" class="block mt-4 w-64 h-10 text-white bg-red-700 rounded-lg hover:bg-red-600"> <!-- <button v-if="store.state.server" :disabled="streamid === ''" @click.prevent="serverDo" class="block mt-4 w-64 h-10 text-white bg-red-700 rounded-lg hover:bg-red-600">
Stop overlay Stop overlay
</button> --> </button> -->
@ -66,9 +70,13 @@ export default {
const check = async () => { const check = async () => {
const status = await window.ipcRenderer.invoke('status') const status = await window.ipcRenderer.invoke('status')
const relayStatus = await window.ipcRenderer.invoke('relayStatus')
if (status) { if (status) {
store.state.server = true store.state.server = true
} }
if (relayStatus) {
store.state.relay = true
}
} }
check() check()
@ -124,12 +132,23 @@ export default {
} }
} }
const relayDo = () => {
if (!store.state.relay) {
window.ipcRenderer.invoke('relay', true)
store.state.relay = true
} else {
store.state.relay = false
window.ipcRenderer.invoke('relay', false);
}
}
return { return {
store, store,
nav, nav,
streamid, streamid,
serverDo, serverDo,
relayDo,
} }
}, },
} }

Loading…
Cancel
Save