我尝试使用 vue-socket.io 从 VUE 客户端连接到两个不同的 Web 套接字服务器像这样:
import { store } from '@/store/store'
import { store2 } from '@/store/store2'
Vue.use(Vuex)
Vue.use(VueSocketio, 'http://192.168.1.101:8000', store)
Vue.use(VueSocketio, 'http://192.168.1.102:8001', store2)
我创建了 2 个存储文件
store.js
和 store2.js
其实第一个
Vue.use(VueSocketio…
线将连接并正常工作,但第二条不会。所以在上面的例子中 Port 8000
连接但不连接 Port 8001
.当我交换两行时 Port 8001
将连接但 Port 8000
将不会。我发现的所有示例都只处理一个 Web 套接字服务器,并且工作正常。
使用 VUE-Client 连接到多个 Web-socket 服务器的最佳实践是什么?
知道我如何连接两者吗?
最佳答案
你可以在 Github 上找到解决方案:
https://github.com/MetinSeylan/Vue-Socket.io/pull/98
添加多个命名空间/套接字连接
let connectObj = {
notification: 'http://localhost:5000/notification',
client: 'http://localhost:5000/client'
}
Vue.use(VueSocketio, connectObj, store)
这增加了在单个 Vue 实例中监听多个套接字实例(对于前命名空间)的能力。如果连接作为对象传递,则可以使用键来识别并因此能够隔离监听器。
关于vue.js - vue-socket.io 如何连接多个 websocket 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51726558/