nathantsoi / vue-native-websocket

native websocket with vuex integration
943 stars 162 forks source link

How to connect two different url in same time #98

Open linwh203 opened 5 years ago

linwh203 commented 5 years ago

I need to subscribe socket message from two different url,
Vue.use(VueNativeSock, url_one), Then how to connect the second url?

kyoto44 commented 5 years ago

+1

enrtama commented 5 years ago

+1

chrisblosser commented 4 years ago

+1

mykhailotarabtsov commented 4 years ago

Hello, @linwh203 have you solved that issue?

WangShayne commented 4 years ago

main.js

Vue.use(VueNativeSock, 'ws://localhost:9090', {
  connectManually: true,
})

temp.vue

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

SangeCoder commented 4 years ago

@WangShayne how to use this.disconnect() ? thanks!

hexagen48 commented 4 years ago

@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()

1061750360 commented 1 year ago

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 }`