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

Api updates (#7)

* move all instantiation options into the opts hash

* 2.0.0
This commit is contained in:
Nathan
2017-07-07 09:47:01 -07:00
committed by GitHub
parent b62cc5a27c
commit 46e251dcbc
8 changed files with 68 additions and 21 deletions
+29 -10
View File
@@ -13,31 +13,46 @@ npm install vue-native-websocket --save
```
## Usage
#### Configuration
Automatic socket connection from an URL string
``` js
import VueNativeSock from 'vue-native-websocket'
Vue.use(VueNativeSock, 'ws://localhost:9090')
```
Set sub-protocol, this is optional option and default is empty string.
``` js
import VueNativeSock from 'vue-native-websocket'
Vue.use(VueNativeSock, 'ws://localhost:9090', 'my-protocol')
```
Enable Vuex integration, where `'./store'` is your local apps store:
``` js
import store from './store'
Vue.use(VueNativeSock, 'ws://localhost:9090', store)
Vue.use(VueNativeSock, 'ws://localhost:9090', { store: store })
```
Set sub-protocol, this is optional option and default is empty string.
``` js
import VueNativeSock from 'vue-native-websocket'
Vue.use(VueNativeSock, 'ws://localhost:9090', { protocol: 'my-protocol' })
```
Optionally enable JSON message passing:
``` js
Vue.use(VueNativeSock, 'ws://localhost:9090', { format: 'json' })
```
JSON message passing with a store:
``` js
import store from './store'
Vue.use(VueNativeSock, 'ws://localhost:9090', store, {format: 'json'})
Vue.use(VueNativeSock, 'ws://localhost:9090', { store: store, format: 'json' })
```
#### On Vuejs instance usage
``` js
var vm = new Vue({
methods: {
@@ -52,13 +67,17 @@ var vm = new Vue({
```
#### Dynamic socket event listeners
Create a new listener
Create a new listener, for example:
``` js
this.$options.sockets.event_name = (data) => console.log(data)
this.$options.sockets.onmessage = (data) => console.log(data)
```
Remove existing listener
``` js
delete this.$options.sockets.event_name
delete this.$options.sockets.onmessage
```
#### Vuex Store integration