-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (38 loc) · 1.16 KB
/
server.js
File metadata and controls
45 lines (38 loc) · 1.16 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
const { createServer } = require('http')
const { readFile } = require('fs')
const { resolve } = require('path')
const { Server } = require('socket.io')
const { createStateMiddleware } = require('../index')
const SERVER_PORT = 3000
const staticServer = (req, res) => {
const basePath = resolve(__dirname)
const serveFile = (path) => {
readFile(path, (err, data) => {
if (err) {
res.writeHead(500)
res.end(err)
}
res.writeHead(200)
res.end(data)
})
}
console.log(`[http server] serving ${req.url}`)
if ( ['/', '/index.html'].includes(req.url)) {
serveFile(resolve(basePath, 'index.html'))
} else if( req.url === '/client.js' ) {
serveFile(resolve(basePath, 'client.js'))
} else {
console.log(`[http server] not found: ${req.url}`)
res.writeHead(404)
res.end()
}
}
const httpServer = createServer(staticServer)
const io = new Server(httpServer, { path: '/socket.io/' })
io.use(createStateMiddleware())
io.on("connection", (socket) => {
console.log("[socket.io] Connected.")
});
httpServer.listen(SERVER_PORT, () => {
console.log(`[http server] started on http://localhost:${SERVER_PORT}`)
});