-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (59 loc) · 1.77 KB
/
Copy pathserver.js
File metadata and controls
72 lines (59 loc) · 1.77 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const helmet = require('helmet')
const morgan = require('morgan')
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server)
let SONORE_MSG = null
app.use(helmet())
app.use(bodyParser.json())
app.use(cors())
app.use(morgan('combined'))
// POST version
// app.post('/', (req, res) => {
// if (req.header('Authorization') !== process.env.SONORE_SECRET_KEY) {
// res.status(401)
// res.send('Unauthorized access')
// return
// }
// SONORE_MSG = req.body.data
// console.log('SONORE_MSG is now', SONORE_MSG)
// io.emit('data', { data: SONORE_MSG })
// })
// debug only
app.get('/', (req, res) => {
res.send('OK')
})
const port = process.env.SONORE_PORT || 3001
server.listen(port, () => {
console.log(`listening on port ${port}`)
})
io.on('connection', socket => {
console.log('connected client')
socket.on('setdata', data => {
if (socket.handshake.query && socket.handshake.query.producer === '1') {
const secret = socket.handshake.headers.authorization
if (secret === process.env.SONORE_SECRET_KEY) {
SONORE_MSG = data
io.emit('data', { data: SONORE_MSG })
}
} else {
console.log('failed authentication to update sonore data')
}
})
socket.on('setcolor', data => {
if (socket.handshake.query && socket.handshake.query.producer === '1') {
const secret = socket.handshake.headers.authorization
if (secret === process.env.SONORE_SECRET_KEY) {
io.emit('color', { data })
}
} else {
console.log('failed authentication to update sonore data')
}
})
})