我正在使用Strophe.js通过websockets连接到XMPP服务器。下面是当连接的用户收到一条消息时,我得到的示例响应:
<message xmlns='jabber:client' xml:lang='en' to='agent@chat.domain.com/6665193359253278721998' from='client@chat.domain.com/Mac' type='chat' id='purple42fccc5c'>
<archived by='agent@chat.domain.com' id='1557026681122740' xmlns='urn:xmpp:mam:tmp'/>
<stanza-id by='agent@chat.domain.com' id='1557026681122740' xmlns='urn:xmpp:sid:0'/>
<active xmlns='http://jabber.org/protocol/chatstates'/>
<body>
1
</body>
</message>我查了文档,但我找不到关于这个主题的任何有用的东西。Strophe有没有内置的方法来从不同类型的消息中提取我需要的数据?或者我还需要别的东西吗?
发布于 2019-05-17 05:12:59
一旦创建了连接,您需要定义钩子来接收消息并能够与其交互:
connection.addHandler(onMessage, null, 'message', 'chat');
connection.addHandler(onMessage, null, 'message', 'groupchat');然后,您只需要定义onMessage函数。
onMessge: function(stanza) {
$stanza = $(stanza);
messageId = $stanza.attr('id') || null;
to = $stanza.attr('to');
from = $stanza.attr('from').toLowerCase();
barejid = Strophe.getBareJidFromJid(from);
type = $stanza.attr('type');
bodies = $stanza.find('body');
body = bodies.length ? Strophe.xmlunescape(Strophe.getText(bodies[0])) : '';
....
}希望这能帮上忙。
https://stackoverflow.com/questions/56022820
复制相似问题