-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (34 loc) · 1.04 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
const http = require('http')
const next = require('next')
const { Server } = require('socket.io')
const production = process.env.NODE_ENV === 'production'
const hostname = 'localhost'
const port = process.env.PORT || 3000
const app = next({ production, hostname, port })
const handler = app.getRequestHandler()
app.prepare().then(() => {
const httpServer = http.createServer(handler)
const socketIO = new Server(httpServer, {
cors: {
origin: '*',
methods: ['POST', 'GET'],
},
})
socketIO.on('connection', (socket) => {
console.log('Client connected')
socket.on('webhook-received', (data) => {
socketIO.emit('send-data', data)
})
socket.on('disconnect', () => {
console.log('Client disconnected')
})
})
httpServer
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`)
})
})