forked from boltgolt/boltobserv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
49 lines (43 loc) · 1.17 KB
/
socket.js
File metadata and controls
49 lines (43 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const fs = require("fs")
const path = require("path")
const WebSocket = require("ws")
const config = require("./loadconfig")()
const wss = new WebSocket.Server({
port: config.browser.ports.socket
})
// Will contain the filenames of all rendered scripts
let scripts = []
// Get a list of available renderers
let renderers = fs.readdirSync(path.join(__dirname, "renderers"))
for (let renderer of renderers) {
// Skip renderers that start with a "_", as they are only helpers
if (renderer.slice(0, 1) == "_") continue
// Load in the render module
scripts.push(renderer)
}
// When a new connection is established
wss.on("connection", ws => {
// Send a packet so the client has a config and knows what scripts to load
ws.send(JSON.stringify({
type: "welcome",
data: {
scripts: scripts,
config: {
browser: config.browser,
radar: config.radar,
autozoom: config.autozoom
}
}
}))
})
// When a packet needs to be sent
process.on("message", data => {
// Format the package as a string
let string = JSON.stringify(data)
// Send it to all open connections
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(string)
}
})
})