【问题标题】:Stanza.io send message with customer attributes in ReactJSStanza.io 在 ReactJS 中发送带有客户属性的消息
【发布时间】:2018-04-25 08:37:22
【问题描述】:

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

【问题讨论】:

    标签: reactjs stanza.io


    【解决方案1】:

    假设我必须发送一条名为 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

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

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多