This repository has been archived by the owner on Nov 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
68 lines (54 loc) · 1.72 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
61
62
63
64
65
66
67
68
require('dotenv').config()
const next = require('next')
const path = require('path')
const routes = require('./routes')
const port = parseInt(process.env.PORT, 10) || 3000
const socketPort = parseInt(process.env.SOCKET_PORT, 10) || 3002
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
// const handle = app.getRequestHandler()
const handle = routes.getRequestHandler(app, ({req, res, route, query}) => {
app.render(req, res, '/', query)
app.render(req, res, route.page, query)
})
const express = require('express')
const server = express()
// const socketServer = require('http').Server(server)
const io = require('socket.io').listen(socketPort)
let users = []
let time = 0
app.prepare()
.then(() => {
io.on('connection', socket => {
socket.emit('setUser', users)
socket.emit('getTime', time)
socket.on('examStart', data => {
time = data
io.emit('examStart', {status: 'start'})
})
socket.on('getUser', () => {
socket.emit('setUser', users)
})
socket.on('getTime', () => {
socket.emit('getTime', time)
})
socket.on('finish', id => {
let index = users.findIndex(u => u === id)
if (index < 0) {
users.push(id)
}
io.sockets.emit('setUser', users)
})
})
server.get('/robot.txt', (req, res) => {
return res.sendFile(path.join(__dirname, './static', 'robot.txt'))
})
server.get('/static/img/favicon/*', (req, res) => {
return res.sendFile(path.join(__dirname, req.url))
})
server.use(handle)
server.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})