-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (35 loc) · 1.19 KB
/
Copy pathserver.js
File metadata and controls
42 lines (35 loc) · 1.19 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
const { createServer } = require('node:http')
const next = require('next')
const { Server } = require('socket.io')
// require for the socketOn function.
const socketOnHandler = require('./src/services/socketOn')
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 3000
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port })
const handler = app.getRequestHandler()
app.prepare().then(() => {
const httpServer = createServer(handler)
const io = new Server(httpServer, {
cors: {
origin: 'http://localhost:3000',
},
})
io.on('connection', (socket) => {
console.log('\n*******************************************************')
console.log('>> server socket.id', socket.id)
console.log('*******************************************************\n')
socketOnHandler(io, socket)
})
httpServer
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log('*******************************************************')
console.log(`>> Ready on http://${hostname}:${port}`)
console.log('*******************************************************\n')
})
})