【问题标题】:adding custom attribute to message not storing message in Archive table on server using stanza.io使用 stanza.io 向消息添加自定义属性而不将消息存储在服务器上的存档表中
【发布时间】:2018-02-05 11:02:57
【问题描述】:

我正在研究 ionic-framework 并且我正在使用 stanza.io 库来实现与 xmpp 服务器的聊天,我想在发送消息时添加一些自定义属性,因为我已经按照创建插件的步骤进行操作。我的代码如下...

sendMsg() {
    console.log("Sending message");


    function customMessage(client, stanzas) {
      const NS = 'http://www.w3.org/2005/Atom';
      var types = stanzas.utils;

      const messageAttribute = stanzas.define({
        name: 'messageAttribute',
        element: 'messageAttribute',
        namespace: NS,
        fields: {
          title: types.textSub(NS, 'title'),
          summary: types.textSub(NS, 'summary'),
          published: types.textSub(NS, 'published'),
          updated: types.textSub(NS, 'updated'),
          cont: types.textSub(NS, 'cont')
        }
      });

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

    this.client.use(customMessage);

    this.client.sendMessage({
      to: this.recep,
      body: "",
      messageAttribute: {
        'title': "some title",
        'summary': "message",
        'published': "time stamp here",
        'updated': "time stamp here",
        'cont': "cht"
      }
    });
   console.log("Message sent " + this.sMsg);
  }

但是这样做消息不会存储在服务器上的存档表中。这将产生从服务器获取历史记录的问题。如果我们使用简单的代码,那么消息将存储在服务器上的存档表中。简单代码如下..

this.client.sendMessage({
      to: this.recep,
      body: this.sMsg    
    });

在简单的代码中,我们只能在正文中将消息作为字符串发送。谁能帮我解决这个问题?

【问题讨论】:

    标签: ionic-framework ionic2 xmpp ionic3 stanza.io


    【解决方案1】:

    我的服务器只归档包含正文元素和文本的消息,这是一种非常常见的归档配置。一个技巧是尝试包含一个虚拟正文文本来触发消息存档,但您必须检查服务器是否正在存储和返回完整的节,或者只是提取和保存正文。

    通过扩展 Stanza 以包含其他字段,正确完成所有操作,但需要调整服务器以获得我想要的。从here确认。

    【讨论】:

      【解决方案2】:

      您需要在消息节中添加一个额外的参数store,使消息默认存储在存档表中。

      const store = stanzas.define({
        name: 'store',
        element: 'store',
        namespace: 'urn:xmpp:hints'
      }); 
      
      stanzas.withMessage(Message => {
        stanzas.extend(Message, store);
      });
      

      在消息节中发送存储属性为真

      this.client.sendMessage({
        to: this.recep,
        body: this.sMsg,
        store: true
      });
      

      您应该在消息节中看到 store,例如

      <store xmlns='urn:xmpp:hints'/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 2015-01-23
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 2016-05-07
        • 1970-01-01
        相关资源
        最近更新 更多