mirror of
https://github.com/tenrok/vue-native-websocket.git
synced 2026-05-17 05:59:39 +03:00
Merge feature/manually-connect-and-disconnect into master
This commit is contained in:
@@ -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
|
||||
|
||||
``` js
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+18
-2
@@ -6,9 +6,25 @@ export default {
|
||||
install (Vue, connection, opts = {}) {
|
||||
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({
|
||||
created () {
|
||||
|
||||
Reference in New Issue
Block a user