Stanza.io 在 ReactJS 中发送带有客户属性的消息

Stanza.io send message with customer attributes in ReactJS

我想在 ReactJS / Stanza.io 中使用自定义属性发送消息。我无法让它工作。有什么好的工作示例吗?

假设我必须发送一条自定义属性名为 custom 的消息:

<message to="tom@example" id="273z4-567" type="chat" from="john@example"> 
   <body>hi</body> 
   <custom xmlns="xmpp:customAttr" layout="testLayout"> // custom attribute
      <value>1234</value> 
   </custom>
</message>

你可以这样做:

const XMPP = require('stanza.io');

let client = XMPP.createClient({}); // obj with config
client.use(this.setCustomMessageAttributes());


setCustomMessageAttributes() {
    const NS = 'xmpp:customAttr';
    const customAttribute = stanzas.define({
        name: 'custom',
        element: 'custom',
        namespace: NS,
        fields: {
            value: stanzas.utils.textSub(NS, 'value'),
            layout: stanzas.utils.attribute('layout')
        }
    });

    stanzas.withMessage((Message) => {
        stanzas.extend(Message, customAttribute);
    });
}

您可以发送消息

client.sendMessage({
    to: "jid",
    body: "hi",
    custom: {
        value: "1234",
        layout: "testLayout"
    }
});

也可以参考https://github.com/legastero/stanza.io/blob/master/docs/Create_Plugin.md

如果您仍然遇到问题,请在此处粘贴您的代码。