2017-07-10 07:03:18 +00:00
|
|
|
module.exports = function () {
|
|
|
|
const server = require('http').createServer(this.nuxt.renderer.app)
|
|
|
|
const io = require('socket.io')(server)
|
|
|
|
|
|
|
|
// overwrite nuxt.listen()
|
|
|
|
this.nuxt.listen = (port, host) => new Promise((resolve) => server.listen(port || 3000, host || 'localhost', resolve))
|
|
|
|
// close this server on 'close' event
|
2018-03-15 09:49:14 +00:00
|
|
|
this.nuxt.hook('close', () => new Promise((resolve) => server.close(resolve)))
|
2017-07-10 07:03:18 +00:00
|
|
|
|
|
|
|
// Add socket.io events
|
|
|
|
let messages = []
|
|
|
|
io.on('connection', (socket) => {
|
|
|
|
socket.on('last-messages', function (fn) {
|
|
|
|
fn(messages.slice(-50))
|
2017-10-20 07:13:17 +00:00
|
|
|
})
|
2017-07-10 07:03:18 +00:00
|
|
|
socket.on('send-message', function (message) {
|
|
|
|
messages.push(message)
|
|
|
|
socket.broadcast.emit('new-message', message)
|
|
|
|
})
|
2017-10-20 07:13:17 +00:00
|
|
|
})
|
|
|
|
}
|