-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
60 lines (49 loc) · 1.89 KB
/
server.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
const { createServer } = require('http')
const express = require('express')
const next = require('next')
const WebSocket = require('ws')
// References:
// - https://nextjs.org/docs/advanced-features/custom-server
// - https://github.com/vercel/next.js/tree/canary/examples/custom-server-express
// - https://github.com/websockets/ws
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const nextApp = next({ dev })
const nextHandler = nextApp.getRequestHandler()
nextApp.prepare().then(() => {
// Express server - for handling incoming HTTP requests from Gravio
const expressApp = express()
// Node http server - added to for integrating WebSocket server
const server = createServer(expressApp)
// WebSocket server - for sending realtime updates to UI
const wss = new WebSocket.Server({ server })
// HTTP route for handling 'Yes' button source requests:
expressApp.get('/buttons/yes', function (req, res) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
// Note: we add a `time` attribute to help with the UI state management
client.send(JSON.stringify({ type: 'buttons:yes', time: new Date() }))
}
})
res.send('buttons:yes')
})
// HTTP route for handling 'No' button source requests:
expressApp.get('/buttons/no', function (req, res) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
// Note: we add a `time` attribute to help with the UI state management
client.send(JSON.stringify({ type: 'buttons:no', time: new Date() }))
}
})
res.send('buttons:no')
})
// To handle Next.js routing
expressApp.all('*', (req, res) => {
return nextHandler(req, res)
})
// Start the server!
server.listen(port, (err) => {
if (err) throw err
console.log(`Ready on http://127.0.0.1:${port}`)
})
})