【问题标题】:Strophe.js on-message handler doesn't triggerStrophe.js 消息处理程序不会触发
【发布时间】:2021-02-07 06:20:46
【问题描述】:

我添加到连接中的 Strophe onMessage 处理程序似乎不会在发送消息时触发。我似乎找不到问题所在。我也找不到很多其他信息,而且我找到的信息似乎表明我的代码是正确的。我可以发送消息,所以我知道连接正常,但我无法接收消息。

我这样创建连接,如果建立了新连接,则调用登录函数:

XMPPChatConnection() {
    if (this.#connection === undefined) {

        this.#connection = new XMPPHelper.Strophe.Connection(
            "wss://xxxxxxxxxxxxxxxxxxxxxxx", 
            {protocol: "wss"}
        );

        this.login();
    }
  return this.#connection;
}

登录函数调用 chatListeners 函数,该函数应设置用户登录时所需的所有侦听器:

login() {
    let jid = this.composeJabberIdentifier();
    let token = this.getXMPPToken();

    this.#connection.connect(jid, token, (status) => {
      if (status === XMPPHelper.Strophe.Status.CONNECTED) {
        this.chatListeners();
      }
   });
}

messageListener 是一个导入函数,目前只包含一个控制台日志:

import messageListener from "../classes/listeners/xmpp/chat/messageListener";

chatListeners() {
    this.XMPPChatConnection().addHandler(messageListener, null, 'message', 'chat');
}

消息监听器:

export default function messageListener(message) {
    console.log(message);
}

我做错了什么?

【问题讨论】:

    标签: javascript xmpp ejabberd strophe


    【解决方案1】:

    所以我找到了问题的原因。我正在使用 Xabber 客户端发回消息,但结果是 Xabber 将消息发送到了错误的资源。

    除此之外,我应该在登录后将我的状态设置为 >= 0。

    this.XMPPChatConnection().send($pres().c("priority").t("0"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      相关资源
      最近更新 更多