mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat: update with-sockets example
This commit is contained in:
parent
1453a98b5f
commit
7cd22a44e7
@ -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)
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -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"
|
||||
},
|
||||
|
@ -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)
|
||||
})
|
||||
});
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user