Strophe.js 的自定义 XMPP 消息

custom XMPP messages with Strophe.js

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

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

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

在 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 文档如何创建消息。

使用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);
}