mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
examples(with-sockets): fix cannot read property 'app' of undefined on nuxt build (#6586)
This commit is contained in:
parent
b031fac121
commit
b8e1a7170d
@ -2,23 +2,25 @@ import http from 'http'
|
|||||||
import socketIO from 'socket.io'
|
import socketIO from 'socket.io'
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
const server = http.createServer(this.nuxt.renderer.app)
|
this.nuxt.hook('render:before', (renderer) => {
|
||||||
const io = socketIO(server)
|
const server = http.createServer(this.nuxt.renderer.app)
|
||||||
|
const io = socketIO(server)
|
||||||
|
|
||||||
// overwrite nuxt.server.listen()
|
// overwrite nuxt.server.listen()
|
||||||
this.nuxt.server.listen = (port, host) => new Promise(resolve => server.listen(port || 3000, host || 'localhost', resolve))
|
this.nuxt.server.listen = (port, host) => new Promise(resolve => server.listen(port || 3000, host || 'localhost', resolve))
|
||||||
// close this server on 'close' event
|
// close this server on 'close' event
|
||||||
this.nuxt.hook('close', () => new Promise(server.close))
|
this.nuxt.hook('close', () => new Promise(server.close))
|
||||||
|
|
||||||
// Add socket.io events
|
// Add socket.io events
|
||||||
const messages = []
|
const 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) {
|
||||||
|
messages.push(message)
|
||||||
|
socket.broadcast.emit('new-message', message)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
socket.on('send-message', function (message) {
|
}
|
||||||
messages.push(message)
|
|
||||||
socket.broadcast.emit('new-message', message)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user