使用stanza.io将自定义属性添加到不在服务器上的存档表中存储消息的消息

问题描述 投票:2回答:2

我正在研究离子框架,我正在使用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);
  }

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

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

在简单的代码中,我们只能将消息作为字符串发送到正文中。任何人都可以帮我解决这个问题吗?

ionic-framework ionic2 xmpp ionic3 stanza.io
2个回答
2
投票

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

通过扩展Stanza以包含其他字段来完成所有操作,但需要调整服务器以获得我想要的内容。来自here证实。


0
投票

您需要在消息节中添加一个额外的param存储,这使得消息默认存储在Archive表中。

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

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

在消息节中将store属性发送为true

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

您应该在消息节中看到商店

<store xmlns='urn:xmpp:hints'/>
© www.soinside.com 2019 - 2024. All rights reserved.