mirror of
https://github.com/tenrok/vue-native-websocket.git
synced 2026-06-23 00:10:36 +03:00
Add reconnect (#25)
* add ws reconnect * added in readme text about reconnect
This commit is contained in:
@@ -51,6 +51,16 @@ import store from './store'
|
|||||||
Vue.use(VueNativeSock, 'ws://localhost:9090', { store: store, format: 'json' })
|
Vue.use(VueNativeSock, 'ws://localhost:9090', { store: store, format: 'json' })
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Enable ws reconnect automatically:
|
||||||
|
|
||||||
|
``` js
|
||||||
|
Vue.use(VueNativeSock, 'ws://localhost:9090', {
|
||||||
|
reconnection: true, // (Boolean) whether to reconnect automatically (false)
|
||||||
|
reconnectionAttempts: 5, // (Number) number of reconnection attempts before giving up (Infinity),
|
||||||
|
reconnectionDelay: 3000, // (Number) how long to initially wait before attempting a new (1000)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
#### On Vuejs instance usage
|
#### On Vuejs instance usage
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
@@ -102,6 +112,8 @@ Update state in the open, close and error callbacks. You can also check the sock
|
|||||||
|
|
||||||
Handle all the data in the `SOCKET_ONMESSAGE` mutation.
|
Handle all the data in the `SOCKET_ONMESSAGE` mutation.
|
||||||
|
|
||||||
|
Reconect events will commit mutations `SOCKET_RECONNECT` and `SOCKET_RECONNECT_ERROR`.
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
@@ -113,6 +125,7 @@ export default new Vuex.Store({
|
|||||||
socket: {
|
socket: {
|
||||||
isConnected: false,
|
isConnected: false,
|
||||||
message: '',
|
message: '',
|
||||||
|
reconnectError: false,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mutations:{
|
mutations:{
|
||||||
@@ -128,7 +141,14 @@ export default new Vuex.Store({
|
|||||||
// default handler called for all methods
|
// default handler called for all methods
|
||||||
SOCKET_ONMESSAGE (state, message) {
|
SOCKET_ONMESSAGE (state, message) {
|
||||||
state.message = message
|
state.message = message
|
||||||
}
|
},
|
||||||
|
// mutations for reconnect methods
|
||||||
|
[ws.WS_RECONNECT](state, count) {
|
||||||
|
console.info(state, count)
|
||||||
|
},
|
||||||
|
[ws.WS_RECONNECT_ERROR](state) {
|
||||||
|
state.socket.reconnectError = true;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "vue-native-websocket",
|
"name": "vue-native-websocket",
|
||||||
"version": "2.0.3",
|
"version": "2.0.4",
|
||||||
"description": "native websocket implemantation for vuejs and vuex",
|
"description": "native websocket implemantation for vuejs and vuex",
|
||||||
"main": "dist/build.js",
|
"main": "dist/build.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ export default {
|
|||||||
},
|
},
|
||||||
beforeDestroy () {
|
beforeDestroy () {
|
||||||
let sockets = this.$options['sockets']
|
let sockets = this.$options['sockets']
|
||||||
|
clearTimeout(observer.reconnectTimeoutId)
|
||||||
|
|
||||||
if (sockets) {
|
if (sockets) {
|
||||||
Object.keys(sockets).forEach((key) => {
|
Object.keys(sockets).forEach((key) => {
|
||||||
|
|||||||
@@ -3,7 +3,17 @@ import Emitter from './Emitter'
|
|||||||
export default class {
|
export default class {
|
||||||
constructor (connectionUrl, opts = {}) {
|
constructor (connectionUrl, opts = {}) {
|
||||||
this.format = opts.format && opts.format.toLowerCase()
|
this.format = opts.format && opts.format.toLowerCase()
|
||||||
|
this.connectionUrl = connectionUrl
|
||||||
|
this.opts = opts
|
||||||
|
|
||||||
|
this.reconnection = this.opts.reconnection || false
|
||||||
|
this.reconnectionAttempts = this.opts.reconnectionAttempts || Infinity
|
||||||
|
this.reconnectionDelay = this.opts.reconnectionDelay || 1000
|
||||||
|
this.reconnectTimeoutId = 0
|
||||||
|
this.reconnectionCount = 0
|
||||||
|
|
||||||
this.connect(connectionUrl, opts)
|
this.connect(connectionUrl, opts)
|
||||||
|
|
||||||
if (opts.store) { this.store = opts.store }
|
if (opts.store) { this.store = opts.store }
|
||||||
this.onEvent()
|
this.onEvent()
|
||||||
}
|
}
|
||||||
@@ -16,13 +26,36 @@ export default class {
|
|||||||
this.WebSocket.sendObj = (obj) => this.WebSocket.send(JSON.stringify(obj))
|
this.WebSocket.sendObj = (obj) => this.WebSocket.send(JSON.stringify(obj))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return this.WebSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
reconnect () {
|
||||||
|
if (this.reconnectionCount <= this.reconnectionAttempts) {
|
||||||
|
this.reconnectionCount++
|
||||||
|
clearTimeout(this.reconnectTimeoutId)
|
||||||
|
|
||||||
|
this.reconnectTimeoutId = setTimeout(() => {
|
||||||
|
if (this.store) { this.passToStore('SOCKET_RECONNECT', this.reconnectionCount) }
|
||||||
|
|
||||||
|
this.connect(this.connectionUrl, this.opts)
|
||||||
|
this.onEvent()
|
||||||
|
}, this.reconnectionDelay)
|
||||||
|
} else {
|
||||||
|
if (this.store) { this.passToStore('SOCKET_RECONNECT_ERROR', true) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onEvent () {
|
onEvent () {
|
||||||
['onmessage', 'onclose', 'onerror', 'onopen'].forEach((eventType) => {
|
['onmessage', 'onclose', 'onerror', 'onopen'].forEach((eventType) => {
|
||||||
this.WebSocket[eventType] = (event) => {
|
this.WebSocket[eventType] = (event) => {
|
||||||
Emitter.emit(eventType, event)
|
Emitter.emit(eventType, event)
|
||||||
|
|
||||||
if (this.store) { this.passToStore('SOCKET_' + eventType, event) }
|
if (this.store) { this.passToStore('SOCKET_' + eventType, event) }
|
||||||
|
|
||||||
|
if (this.reconnection && this.eventType === 'onopen') { this.reconnectionCount = 0 }
|
||||||
|
|
||||||
|
if (this.reconnection && eventType === 'onclose') { this.reconnect(event) }
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user