Open linwh203 opened 5 years ago
+1
+1
+1
Hello, @linwh203 have you solved that issue?
Vue.use(VueNativeSock, 'ws://localhost:9090', {
connectManually: true,
})
created
this.$connect(wsRUL1, { format: 'json'})
this.ws1 = this.$socket
this.$connect(wsRUL2, { format: 'json'})
this.ws2 = this.$socket
this.ws1.onmessage = data => { console.log(data) }
this.ws2.onmessage = data => { console.log(data) }
it's work for me ---warning--- I cannot guarantee a stable connection
@WangShayne how to use this.disconnect() ? thanks!
@Siffre i am also trying to use multiple connections and close() functions seems to work. I have another approach in a code, but try to use this.ws1.close()
you can edit the "vue-native-websocket" source code like this picture. when you have multiple websocket,you can not close all,because the source code just provide one instance. and then you must copy 'Emitter.js'、'Main.js'、’Observer.js‘ to your project。 and next you should edit the import path . like "import VueNativeSock from './common/vue-native-websocket/Main'". './common/vue-native-websocket/Main' : this is my project path.
Btw: this fucking editor i am not good at it.
1: `let observer = null let observerList = []. // Added
opts.$setInstance = (wsInstance) => {
Vue.prototype.$socket = wsInstance
}`
2: `Vue.prototype.$connect = (connectionUrl = connection, connectionOpts = opts) => { connectionOpts.$setInstance = opts.$setInstance observer = new Observer(connectionUrl, connectionOpts) Vue.prototype.$socket = observer.WebSocket observerList.push(observer) // Added }
Vue.prototype.$disconnect = () => { observerList.forEach(observerItem => { // Added if (observerItem && observerItem.reconnection) { observerItem.reconnection = false } // Added if (observerItem?.WebSocket?.close) { // Added observerItem.WebSocket.close() // Added } // Added }) // Added if (Vue.prototype.$socket) { Vue.prototype.$socket.close() delete Vue.prototype.$socket } let observer = null // Added observerList = [] // Added }`
I need to subscribe socket message from two different url,
Vue.use(VueNativeSock, url_one), Then how to connect the second url?