Only connect to chat when authenticating in the first place

To avoid duplication of the connection, the chat socket is destroyed
upon logging out.
This commit is contained in:
slice 2019-03-10 11:23:27 -07:00
parent a67881b096
commit e618c6ffb0
No known key found for this signature in database
GPG key ID: 1508C19D7436A26D
4 changed files with 15 additions and 5 deletions

View file

@ -89,10 +89,8 @@ const afterStoreSetup = ({ store, i18n }) => {
copyInstanceOption('noAttachmentLinks') copyInstanceOption('noAttachmentLinks')
copyInstanceOption('showFeaturesPanel') copyInstanceOption('showFeaturesPanel')
if ((config.chatDisabled)) { if (config.chatDisabled) {
store.dispatch('disableChat') store.dispatch('disableChat')
} else {
store.dispatch('initializeSocket')
} }
return store.dispatch('setTheme', config['theme']) return store.dispatch('setTheme', config['theme'])

View file

@ -50,7 +50,7 @@ const api = {
}, },
initializeSocket (store) { initializeSocket (store) {
// Set up websocket connection // Set up websocket connection
if (!store.state.chatDisabled && store.state.wsToken) { if (!store.state.chatDisabled) {
const token = store.state.wsToken const token = store.state.wsToken
const socket = new Socket('/socket', {params: {token}}) const socket = new Socket('/socket', {params: {token}})
socket.connect() socket.connect()

View file

@ -1,12 +1,16 @@
const chat = { const chat = {
state: { state: {
messages: [], messages: [],
channel: {state: ''} channel: {state: ''},
socket: null
}, },
mutations: { mutations: {
setChannel (state, channel) { setChannel (state, channel) {
state.channel = channel state.channel = channel
}, },
setSocket (state, socket) {
state.socket = socket
},
addMessage (state, message) { addMessage (state, message) {
state.messages.push(message) state.messages.push(message)
state.messages = state.messages.slice(-19, 20) state.messages = state.messages.slice(-19, 20)
@ -16,8 +20,12 @@ const chat = {
} }
}, },
actions: { actions: {
disconnectFromChat (store) {
store.state.socket.disconnect()
},
initializeChat (store, socket) { initializeChat (store, socket) {
const channel = socket.channel('chat:public') const channel = socket.channel('chat:public')
store.commit('setSocket', socket)
channel.on('new_msg', (msg) => { channel.on('new_msg', (msg) => {
store.commit('addMessage', msg) store.commit('addMessage', msg)
}) })

View file

@ -292,6 +292,7 @@ const users = {
logout (store) { logout (store) {
store.commit('clearCurrentUser') store.commit('clearCurrentUser')
store.dispatch('disconnectFromChat')
store.commit('setToken', false) store.commit('setToken', false)
store.dispatch('stopFetching', 'friends') store.dispatch('stopFetching', 'friends')
store.commit('setBackendInteractor', backendInteractorService()) store.commit('setBackendInteractor', backendInteractorService())
@ -321,6 +322,9 @@ const users = {
if (user.token) { if (user.token) {
store.dispatch('setWsToken', user.token) store.dispatch('setWsToken', user.token)
// Initialize the chat socket.
store.dispatch('initializeSocket')
} }
// Start getting fresh posts. // Start getting fresh posts.