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

@ -15,10 +15,10 @@ module.exports = function () {
io.on('connection', (socket) => { io.on('connection', (socket) => {
socket.on('last-messages', function (fn) { socket.on('last-messages', function (fn) {
fn(messages.slice(-50)) fn(messages.slice(-50))
}); })
socket.on('send-message', function (message) { socket.on('send-message', function (message) {
messages.push(message) messages.push(message)
socket.broadcast.emit('new-message', message) socket.broadcast.emit('new-message', message)
}) })
}); })
} }

View File

@ -7,7 +7,7 @@
}, },
"dependencies": { "dependencies": {
"express": "^4.14.0", "express": "^4.14.0",
"nuxt": "^0.9.5", "nuxt": "latest",
"socket.io": "^1.7.2", "socket.io": "^1.7.2",
"socket.io-client": "^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 app = require('express')()
const server = require('http').createServer(app) const server = require('http').createServer(app)
const io = require('socket.io')(server) const io = require('socket.io')(server)
@ -8,17 +8,14 @@ const isProd = process.env.NODE_ENV === 'production'
// We instantiate Nuxt.js with the options // We instantiate Nuxt.js with the options
let config = require('./nuxt.config.js') let config = require('./nuxt.config.js')
config.dev = !isProd 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) { if (config.dev) {
nuxt.build() const builder = new Builder(nuxt)
.catch((error) => { builder.build()
console.error(error) // eslint-disable-line no-console
process.exit(1)
})
} }
app.use(nuxt.render)
// Listen the server // Listen the server
server.listen(port, '0.0.0.0') server.listen(port, '0.0.0.0')
@ -29,9 +26,9 @@ let messages = []
io.on('connection', (socket) => { io.on('connection', (socket) => {
socket.on('last-messages', function (fn) { socket.on('last-messages', function (fn) {
fn(messages.slice(-50)) fn(messages.slice(-50))
}); })
socket.on('send-message', function (message) { socket.on('send-message', function (message) {
messages.push(message) messages.push(message)
socket.broadcast.emit('new-message', message) socket.broadcast.emit('new-message', message)
}) })
}); })