diff --git a/src/background.js b/src/background.js
index f4e7528..ed71fcf 100644
--- a/src/background.js
+++ b/src/background.js
@@ -7,6 +7,7 @@ import path from 'path'
const isDevelopment = process.env.NODE_ENV !== 'production'
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
protocol.registerSchemesAsPrivileged([
@@ -96,6 +97,10 @@ ipcMain.handle('status', async () => {
return status()
})
+ipcMain.handle('relayStatus', async () => {
+ return relayStatus()
+})
+
ipcMain.handle('server', async (e, arg) => {
if (arg === true) {
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 () => {
console.log('Sending refreshed data')
sendAll()
diff --git a/src/store.js b/src/store.js
index 418d82f..d63cfdf 100644
--- a/src/store.js
+++ b/src/store.js
@@ -24,6 +24,7 @@ const state = reactive({
brackets: [],
overlayStream: null,
server: false,
+ relay: false,
})
let interval = 0
diff --git a/src/utils/relay.js b/src/utils/relay.js
new file mode 100644
index 0000000..a726dcf
--- /dev/null
+++ b/src/utils/relay.js
@@ -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,
+ }))
+ }
+}
\ No newline at end of file
diff --git a/src/utils/server.js b/src/utils/server.js
index a0d2893..9f46cae 100644
--- a/src/utils/server.js
+++ b/src/utils/server.js
@@ -1,4 +1,4 @@
-import ws from 'ws'
+import ws from 'ws'
import { v4 } from 'uuid'
let wss = null
diff --git a/src/views/Home.vue b/src/views/Home.vue
index bf01a62..6091042 100644
--- a/src/views/Home.vue
+++ b/src/views/Home.vue
@@ -24,6 +24,10 @@
Start
overlay
+
@@ -66,9 +70,13 @@ export default {
const check = async () => {
const status = await window.ipcRenderer.invoke('status')
+ const relayStatus = await window.ipcRenderer.invoke('relayStatus')
if (status) {
store.state.server = true
}
+ if (relayStatus) {
+ store.state.relay = true
+ }
}
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 {
store,
nav,
streamid,
serverDo,
+ relayDo,
}
},
}