2018-03-16 16:12:06 +00:00
|
|
|
import http from 'http'
|
|
|
|
import socketIO from 'socket.io'
|
2017-07-10 07:03:18 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
const server = http.createServer(this.nuxt.renderer.app)
|
|
|
|
const io = socketIO(server)
|
|
|
|
|
|
|
|
export default function () {
|
2018-10-30 20:42:53 +00:00
|
|
|
// overwrite nuxt.server.listen()
|
|
|
|
this.nuxt.server.listen = (port, host) => new Promise(resolve => server.listen(port || 3000, host || 'localhost', resolve))
|
2017-07-10 07:03:18 +00:00
|
|
|
// close this server on 'close' event
|
2018-07-20 15:42:31 +00:00
|
|
|
this.nuxt.hook('close', () => new Promise(server.close))
|
2017-07-10 07:03:18 +00:00
|
|
|
|
|
|
|
// Add socket.io events
|
2018-08-08 10:54:05 +00:00
|
|
|
const messages = []
|
2017-07-10 07:03:18 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|