2017-04-22 12:53:48 +00:00
|
|
|
const app = require('express')()
|
|
|
|
const server = require('http').Server(app)
|
|
|
|
const io = require('socket.io')(server)
|
|
|
|
const next = require('next')
|
|
|
|
|
2017-08-10 18:15:46 +00:00
|
|
|
const port = parseInt(process.env.PORT, 10) || 3000
|
2017-04-22 12:53:48 +00:00
|
|
|
const dev = process.env.NODE_ENV !== 'production'
|
|
|
|
const nextApp = next({ dev })
|
|
|
|
const nextHandler = nextApp.getRequestHandler()
|
|
|
|
|
|
|
|
// fake DB
|
2018-06-23 20:17:37 +00:00
|
|
|
const messages = {
|
|
|
|
chat1: [],
|
|
|
|
chat2: []
|
|
|
|
}
|
2017-04-22 12:53:48 +00:00
|
|
|
|
|
|
|
// socket.io server
|
|
|
|
io.on('connection', socket => {
|
2018-12-17 16:34:32 +00:00
|
|
|
socket.on('message.chat1', data => {
|
2018-06-23 20:17:37 +00:00
|
|
|
messages['chat1'].push(data)
|
|
|
|
socket.broadcast.emit('message.chat1', data)
|
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
socket.on('message.chat2', data => {
|
2018-06-23 20:17:37 +00:00
|
|
|
messages['chat2'].push(data)
|
|
|
|
socket.broadcast.emit('message.chat2', data)
|
2017-04-22 12:53:48 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
nextApp.prepare().then(() => {
|
2018-06-23 20:17:37 +00:00
|
|
|
app.get('/messages/:chat', (req, res) => {
|
|
|
|
res.json(messages[req.params.chat])
|
2017-04-22 12:53:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get('*', (req, res) => {
|
|
|
|
return nextHandler(req, res)
|
|
|
|
})
|
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.listen(port, err => {
|
2017-04-22 12:53:48 +00:00
|
|
|
if (err) throw err
|
2017-08-10 18:15:46 +00:00
|
|
|
console.log(`> Ready on http://localhost:${port}`)
|
2017-04-22 12:53:48 +00:00
|
|
|
})
|
|
|
|
})
|