【问题标题】:Why won't this nuxt-socket-io emitter trigger its action?为什么这个 nuxt-socket-io 发射器不会触发它的动作?
【发布时间】:2021-07-20 17:00:35
【问题描述】:

我正在尝试让 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 方法中看到“方法有效”控制台输出,但我从未见过“发出有效”消息。我已经玩了几个小时,复制了 this guide 的说明,但没有成功。

谁能发现我做错了什么?

更新:我尝试从 this example 复制代码,但无法从该 heroku 页面获得响应。所以看来我完全无法发射(即使 $nuxtSocket 似乎可以正常工作并说它已连接)。我能够确认套接字本身已启动并正在运行,因为我能够使用该示例中的滴答声从中获得响应。我将把这个项目的 repo 放到 here 上供查看。

UPDATE2:我做了一个更简单的项目here,它也不能正常运行,但应该更容易检查。

【问题讨论】:

    标签: websocket socket.io nuxt.js vuex


    【解决方案1】:

    事实证明,虽然 nuxt-socket-io 用作 socket.io 的包装器,但您仍然需要实际创建服务器。 This is a good template for how to do this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多