使用Strophe.js自定义XMPP消息



如何使用 Strophe JS 库使用 XMPP 发送自定义消息?

我知道使用$msg( ... );我可以创建一个聊天消息元素并connection.send(m);通过 XMPP 连接发送它。

我需要一种方法来发送消息,不是为了聊天,而是为了"命令"(或其他目的)。

使用 Strophe.js您可以简单地执行以下操作:

function sendCustomMessage(to, from, body, field1, field2) {
    var m = $msg({to: to, from: from, type: 'chat'}).c("body").t(body);     
   // custom data
   m.up().c("data", {xmlns: 'my-custom-data-ns', field1: field1, field2: field2});
   connection.send(m);
}

在 XMPP 中,您可以在 XML 节中添加自定义有效负载,例如:

<message id="xyz" type="chat" to="tojid@com" from="fromjid@com">
    <body>....</body>
    <data xmlns='mycustom-data-ns'
      myField1="bye" myField2="data" />
</message>

检查 Strophe .js文档如何创建该消息。

最新更新