feat: update with-sockets example

This commit is contained in:
Clark Du 2017-10-20 15:13:17 +08:00 committed by Pooya Parsa
parent 1453a98b5f
commit 7cd22a44e7
3 changed files with 13 additions and 16 deletions

View File

@ -9,16 +9,16 @@ module.exports = function () {
// Add `socket.io-client` in vendor
this.addVendor('socket.io-client')
// Add socket.io events
let messages = []
io.on('connection', (socket) => {
socket.on('last-messages', function (fn) {
fn(messages.slice(-50))
});
})
socket.on('send-message', function (message) {
messages.push(message)
socket.broadcast.emit('new-message', message)
})
});
}
})
}

View File

@ -7,7 +7,7 @@
},
"dependencies": {
"express": "^4.14.0",
"nuxt": "^0.9.5",
"nuxt": "latest",
"socket.io": "^1.7.2",
"socket.io-client": "^1.7.2"
},

View File

@ -1,4 +1,4 @@
const Nuxt = require('nuxt')
const { Nuxt, Builder } = require('nuxt')
const app = require('express')()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
@ -8,17 +8,14 @@ const isProd = process.env.NODE_ENV === 'production'
// We instantiate Nuxt.js with the options
let config = require('./nuxt.config.js')
config.dev = !isProd
const nuxt = new Nuxt(config)
app.use(nuxt.render)
// Build only in dev mode
const nuxt = new Nuxt(config)
// Start build process in dev mode
if (config.dev) {
nuxt.build()
.catch((error) => {
console.error(error) // eslint-disable-line no-console
process.exit(1)
})
const builder = new Builder(nuxt)
builder.build()
}
app.use(nuxt.render)
// Listen the server
server.listen(port, '0.0.0.0')
@ -29,9 +26,9 @@ let messages = []
io.on('connection', (socket) => {
socket.on('last-messages', function (fn) {
fn(messages.slice(-50))
});
})
socket.on('send-message', function (message) {
messages.push(message)
socket.broadcast.emit('new-message', message)
})
});
})