-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulator.js
60 lines (51 loc) · 1.54 KB
/
emulator.js
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
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env node
const { config } = require('./lib')
const WebSocket = require('ws')
const http = require('http')
const https = require('https')
const { addGETListener, respondWithFile } = require('./server')
// ---Orb Emulator Server---
let orbEmulatorServer
if (config.DEV_MODE) {
orbEmulatorServer = http.createServer(() => {})
} else {
orbEmulatorServer = https.createServer(config.httpsOptions, () => {})
}
orbEmulatorServer.listen(8888, "0.0.0.0", function() {
console.log('Orb Emulator WebSocket Server is listening on port 8888')
})
let wsOrbEmulatorServer = new WebSocket.Server({
server: orbEmulatorServer,
autoAcceptConnections: true
})
wsOrbEmulatorServer.on('connection', socket => {
socket.binaryType = "arraybuffer"
let id = Math.floor(Math.random() * 1000000000) // change this to uuid?
orbEmulatorConnections[id] = socket
socket.id = id
socket.on('close', () => {
delete orbEmulatorConnections[id]
})
})
let orbEmulatorConnections = {}
function orbEmulatorBroadcast(message) {
for (let id in orbEmulatorConnections) {
orbEmulatorConnections[id].send(message)
}
}
addGETListener((response, orbID, filePath)=>{
if (filePath == '/pixels.json') {
respondWithFile(response, config.PIXELS)
return true
}
})
addGETListener((response, orbID, filePath) => {
if(!orbID || orbID != config.ORB_ID.toLowerCase()) return
if (filePath.includes('dev') || filePath.includes('view')){
respondWithFile(response, "/emulator/emulator.html")
return true
}
})
module.exports = {
orbEmulatorBroadcast
}