yandere_fe/src/modules/chat.js

34 lines
771 B
JavaScript
Raw Normal View History

2017-12-05 02:47:10 -08:00
const chat = {
state: {
messages: [],
2018-02-01 09:18:47 -08:00
channel: {state: ''}
2017-12-05 02:47:10 -08:00
},
mutations: {
setChannel (state, channel) {
state.channel = channel
},
addMessage (state, message) {
state.messages.push(message)
state.messages = state.messages.slice(-19, 20)
},
setMessages (state, messages) {
state.messages = messages.slice(-19, 20)
}
},
actions: {
initializeChat (store, socket) {
const channel = socket.channel('chat:public')
channel.on('new_msg', (msg) => {
store.commit('addMessage', msg)
})
channel.on('messages', ({messages}) => {
store.commit('setMessages', messages)
})
channel.join()
store.commit('setChannel', channel)
}
}
}
export default chat