简体   繁体   English

使用Strophe.js定制XMPP消息

[英]custom XMPP messages with Strophe.js

How can I send custom messages with XMPP using the Strophe JS library? 如何使用Strophe JS库使用XMPP发送自定义消息?

I know that using $msg( ... ); 我知道使用$msg( ... ); I can create a chat message element and connection.send(m); 我可以创建一个聊天消息元素和connection.send(m); send it through XMPP connection. 通过XMPP连接发送它。

I need a way to send messages not for chat but for "command" (or other purpose). 我需要一种方法来发送不是为了聊天而是用于“命令”(或其他目的)的消息。

In XMPP you can add custom payload in the XML stanza, for example: 在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>

Check on Strophe.js docs how to create that msg. 检查Strophe.js文档如何创建该消息。

Using Strophe.js you can simply do: 使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM