问题描述
我尝试使用 vue-socket 从 VUE 客户端连接到两个不同的 web-socket 服务器.io 像这样:
I try to connect to two different web-socket Servers from a VUE-Client with vue-socket.io like this:
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
I have created 2 store files store.js
and store2.js
实际上第一行 Vue.use(VueSocketio...
会连接并正常工作,但第二行不会.所以在上面的例子中 Port 8000
连接但不是 Port 8001
.当我交换两行时Port 8001
会连接但Port 8000
不会.
Actually the first Vue.use(VueSocketio…
line will connect and work properly, but the second will not. So in the upper example Port 8000
connects but not Port 8001
. When I swap the two lines Port 8001
will connect but Port 8000
will not.
我发现的所有示例都只处理一个 Web 套接字服务器,并且工作正常.使用 VUE-Client 连接到多个 Web-socket 服务器的最佳实践是什么?
All Examples I found deal only with one Web-socket Server and that works fine.What is the best practice to connect to multiple Web-socket Servers with a VUE-Client?
知道如何将两者联系起来吗?
Any idea how I get both connected?
推荐答案
你可以在 Github 上找到解决方案:https://github.com/MetinSeylan/Vue-Socket.io/pull/98
You can find the solution on 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 实例中侦听多个套接字实例(对于前命名空间)的能力.如果连接作为对象传递,则可以使用键来识别并因此能够隔离侦听器.
This adds capability to listen to multiple socket instances (for ex- namespaces) in single Vue instance.If connection is passed as an object, the keys can be used to identify and thus will be able to isolate listeners.
这篇关于vue-socket.io 如何连接多个 websocket 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!