mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
Update example
This commit is contained in:
parent
1051b733f9
commit
ad14d7439e
@ -1,5 +1,14 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
build: {
|
build: {
|
||||||
vendor: ['socket.io-client']
|
vendor: ['socket.io-client']
|
||||||
|
},
|
||||||
|
head: {
|
||||||
|
meta: [
|
||||||
|
{ charset: 'utf-8' },
|
||||||
|
{ name: 'viewport', content: 'width=device-width, initial-scale=1' }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
env: {
|
||||||
|
HOST_URL: process.env.HOST_URL || 'http://localhost:3000'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<ul class="pages">
|
<ul class="pages">
|
||||||
<li class="chat page">
|
<li class="chat page">
|
||||||
<div class="chatArea">
|
<div class="chatArea">
|
||||||
<ul class="messages">
|
<ul class="messages" ref="messages">
|
||||||
<li class="message" v-for="message in messages"><i :title="message.date">{{ message.date.split('T')[1].slice(0, -2) }}</i>: {{ message.text }}</li>
|
<li class="message" v-for="message in messages"><i :title="message.date">{{ message.date.split('T')[1].slice(0, -2) }}</i>: {{ message.text }}</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
@ -25,11 +25,17 @@ export default {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
'messages': 'scrollToBottom'
|
||||||
|
},
|
||||||
beforeMount () {
|
beforeMount () {
|
||||||
socket.on('new-message', (message) => {
|
socket.on('new-message', (message) => {
|
||||||
this.messages.push(message)
|
this.messages.push(message)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
mounted () {
|
||||||
|
this.scrollToBottom()
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
sendMessage () {
|
sendMessage () {
|
||||||
if (!this.message.trim()) return
|
if (!this.message.trim()) return
|
||||||
@ -40,6 +46,11 @@ export default {
|
|||||||
this.messages.push(message)
|
this.messages.push(message)
|
||||||
this.message = ''
|
this.message = ''
|
||||||
socket.emit('send-message', message)
|
socket.emit('send-message', message)
|
||||||
|
},
|
||||||
|
scrollToBottom () {
|
||||||
|
this.$nextTick(() => {
|
||||||
|
this.$refs.messages.scrollTop = this.$refs.messages.scrollHeight
|
||||||
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
head: {
|
head: {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import io from 'socket.io-client'
|
import io from 'socket.io-client'
|
||||||
const socket = io('http://localhost:3000')
|
const socket = io(process.env.HOST_URL)
|
||||||
|
|
||||||
export default socket
|
export default socket
|
||||||
|
@ -15,24 +15,22 @@ app.use(nuxt.render)
|
|||||||
if (config.dev) {
|
if (config.dev) {
|
||||||
nuxt.build()
|
nuxt.build()
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error(error) // eslint-disable-line no-console
|
console.error(error) // eslint-disable-line no-console
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen the server
|
// Listen the server
|
||||||
server.listen(port, '0.0.0.0')
|
server.listen(port, '0.0.0.0')
|
||||||
console.log('Server listening on localhost:' + port)
|
console.log('Server listening on localhost:' + port) // eslint-disable-line no-console
|
||||||
|
|
||||||
// Socket.io
|
// Socket.io
|
||||||
let messages = []
|
let messages = []
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
console.log('New connection');
|
|
||||||
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) {
|
||||||
console.log('Message received', message);
|
|
||||||
messages.push(message)
|
messages.push(message)
|
||||||
socket.broadcast.emit('new-message', message)
|
socket.broadcast.emit('new-message', message)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user