2
0
mirror of https://github.com/tenrok/vue-native-websocket.git synced 2026-06-20 10:00:34 +03:00

Merge pull request #59 from OmgImAlexis/master

add support for custom mutation name
This commit is contained in:
Viktor
2018-09-12 16:37:13 +03:00
committed by GitHub
2 changed files with 67 additions and 2 deletions
+61 -1
View File
@@ -37,7 +37,6 @@ import VueNativeSock from 'vue-native-websocket'
Vue.use(VueNativeSock, 'ws://localhost:9090', { protocol: 'my-protocol' })
```
Optionally enable JSON message passing:
``` js
@@ -173,6 +172,67 @@ export default new Vuex.Store({
})
```
##### With custom mutation names
``` js
// store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
export default new Vuex.Store({
state: {
socket: {
isConnected: false,
message: '',
reconnectError: false,
}
},
mutations:{
SOCKET_ONOPEN (state, event) {
state.socket.isConnected = true
},
SOCKET_ONCLOSE (state, event) {
state.socket.isConnected = false
},
SOCKET_ONERROR (state, event) {
console.error(state, event)
},
// default handler called for all methods
SOCKET_ONMESSAGE (state, message) {
state.socket.message = message
},
// mutations for reconnect methods
SOCKET_RECONNECT(state, count) {
console.info(state, count)
},
SOCKET_RECONNECT_ERROR(state) {
state.socket.reconnectError = true;
},
}
})
// index.js
import store from './store'
const mutations = {
SOCKET_ONOPEN: '✅ Socket connected!',
SOCKET_ONCLOSE: '❌ Socket disconnected!',
SOCKET_ONERROR: '❌ Socket Error!!!',
SOCKET_ONMESSAGE: 'Websocket message received',
SOCKET_RECONNECT: 'Websocket reconnected',
SOCKET_RECONNECT_ERROR: 'Websocket is having issues reconnecting..'
};
Vue.use(VueNativeSock, 'ws://localhost:9090', {
store: store,
format: 'json',
mutations: mutations
})
```
##### With `format: 'json'` enabled
All data passed through the websocket is expected to be JSON.