mirror of
https://github.com/tenrok/vue-native-websocket.git
synced 2026-06-24 02:20:35 +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
|
#### On Vuejs instance usage
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+17
-1
@@ -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
|
||||||
|
|
||||||
|
if (opts.connectManually) {
|
||||||
|
Vue.prototype.$connect = function () {
|
||||||
|
observer = new Observer(connection, opts)
|
||||||
Vue.prototype.$socket = observer.WebSocket
|
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 () {
|
||||||
|
|||||||
Reference in New Issue
Block a user