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

Merge feature/manually-connect-and-disconnect into master

This commit is contained in:
Will McNaughton
2018-04-02 15:36:29 -05:00
3 changed files with 31 additions and 3 deletions
+12
View File
@@ -61,6 +61,18 @@ Vue.use(VueNativeSock, 'ws://localhost:9090', {
}) })
``` ```
Manage connection manually:
``` js
Vue.use(VueNativeSock, 'ws://localhost:9090', {
connectManually: true,
})
const vm = new Vue()
vm.$connect()
// do stuff with WebSockets
vm.$disconnect()
```
#### On Vuejs instance usage #### On Vuejs instance usage
``` js ``` js
+1 -1
View File
File diff suppressed because one or more lines are too long
+18 -2
View File
@@ -6,9 +6,25 @@ export default {
install (Vue, connection, opts = {}) { install (Vue, connection, opts = {}) {
if (!connection) { throw new Error('[vue-native-socket] cannot locate connection') } if (!connection) { throw new Error('[vue-native-socket] cannot locate connection') }
let observer = new Observer(connection, opts) let observer = null
Vue.prototype.$socket = observer.WebSocket if (opts.connectManually) {
Vue.prototype.$connect = function () {
observer = new Observer(connection, opts)
Vue.prototype.$socket = observer.WebSocket
}
Vue.prototype.$disconnect = function () {
if (observer && observer.reconnection) { observer.reconnection = false }
if (Vue.prototype.$socket) {
Vue.prototype.$socket.close()
delete Vue.prototype.$socket
}
}
} else {
observer = new Observer(connection, opts)
Vue.prototype.$socket = observer.WebSockekt
}
Vue.mixin({ Vue.mixin({
created () { created () {