为什么这个 nuxt-socket-io 发射器不会触发它的动作?
Why won't this nuxt-socket-io emitter trigger its action?
我正在尝试让 nuxt-socket-io 在我的 NuxtJS 应用程序上运行,但我的发射函数似乎没有触发我的 vuex 存储中的操作。
nuxt.config.js 有以下代码:
modules: [
'@nuxtjs/axios',
'nuxt-socket-io'
],
io: {
sockets: [
{
name: 'main',
url: process.env.WS_URL || 'http://localhost:3000',
default: true,
vuex: {
mutations: [],
actions: [ "subscribeToMirror" ],
emitBacks: []
},
},
]
},
那个 subscribeToMirror 操作存在于我的 vuex 存储中(在 index.js 中):
export const actions = {
async subscribeToMirror() {
console.log('emit worked');
try {
new TopicMessageQuery()
.setTopicId(state.topicId)
.setStartTime(0) // TODO: last 3 days
.subscribe(HederaClient, res => {
console.log("Got response from mirror...");
return res;
});
} catch (error) {
console.error(error);
}
}
};
并且该操作应该由我的 index.vue 脚本中的 emit 触发:
mounted() {
this.socket = this.$nuxtSocket({
name: 'main',
reconnection: false
})
},
methods: {
...mapMutations([
'setEnv',
'initHashgraphClient',
'setTopicId',
'createNewTopicId'
]),
...mapActions([
'createAndSetTopicId'
]),
subscribeToMirror() {
console.log("method worked");
return new Promise((res) => {
this.socket.emit('subscribeToMirror', {}, (resp) => {
console.log(resp)
resolve()
})
})
}
}
虽然我可以从 index.vue 的 subscribeToMirror 方法看到 'method worked' 控制台输出,但我从未看到 'emit worked' 消息。我已经玩了几个小时,从 this guide 复制了说明,但没有成功。
谁能发现我做错了什么?
更新:我尝试从 this example and was unable to get a response from that heroku page. So it appears that I am completely unable to emit (even though $nuxtSocket appears to be functional and says it's connected). I am able to confirm that the socket itself is up and running, as I was able to get responses from it using the ticks from that example. I'm putting the repo for this project up here 复制代码以供查看。
更新 2:我做了一个更简单的项目 here,它也没有正常运行,但应该更容易检查。
事实证明,虽然 nuxt-socket-io 用作 socket.io 东西的包装器,但您仍然需要实际创建服务器。 This is a good template for how to do this
我正在尝试让 nuxt-socket-io 在我的 NuxtJS 应用程序上运行,但我的发射函数似乎没有触发我的 vuex 存储中的操作。
nuxt.config.js 有以下代码:
modules: [
'@nuxtjs/axios',
'nuxt-socket-io'
],
io: {
sockets: [
{
name: 'main',
url: process.env.WS_URL || 'http://localhost:3000',
default: true,
vuex: {
mutations: [],
actions: [ "subscribeToMirror" ],
emitBacks: []
},
},
]
},
那个 subscribeToMirror 操作存在于我的 vuex 存储中(在 index.js 中):
export const actions = {
async subscribeToMirror() {
console.log('emit worked');
try {
new TopicMessageQuery()
.setTopicId(state.topicId)
.setStartTime(0) // TODO: last 3 days
.subscribe(HederaClient, res => {
console.log("Got response from mirror...");
return res;
});
} catch (error) {
console.error(error);
}
}
};
并且该操作应该由我的 index.vue 脚本中的 emit 触发:
mounted() {
this.socket = this.$nuxtSocket({
name: 'main',
reconnection: false
})
},
methods: {
...mapMutations([
'setEnv',
'initHashgraphClient',
'setTopicId',
'createNewTopicId'
]),
...mapActions([
'createAndSetTopicId'
]),
subscribeToMirror() {
console.log("method worked");
return new Promise((res) => {
this.socket.emit('subscribeToMirror', {}, (resp) => {
console.log(resp)
resolve()
})
})
}
}
虽然我可以从 index.vue 的 subscribeToMirror 方法看到 'method worked' 控制台输出,但我从未看到 'emit worked' 消息。我已经玩了几个小时,从 this guide 复制了说明,但没有成功。
谁能发现我做错了什么?
更新:我尝试从 this example and was unable to get a response from that heroku page. So it appears that I am completely unable to emit (even though $nuxtSocket appears to be functional and says it's connected). I am able to confirm that the socket itself is up and running, as I was able to get responses from it using the ticks from that example. I'm putting the repo for this project up here 复制代码以供查看。
更新 2:我做了一个更简单的项目 here,它也没有正常运行,但应该更容易检查。
事实证明,虽然 nuxt-socket-io 用作 socket.io 东西的包装器,但您仍然需要实际创建服务器。 This is a good template for how to do this