关于XMPP格式化文本的研究

来源:互联网 发布:8787端口 编辑:程序博客网 时间:2024/05/21 06:58

协议介绍

XEP-0071 XHTML-IM定义了如何支持Format消息,例如字体,颜色,hyperlink等,格式如下:
<message to='conn@jabber.org/resource1' id='conid74' type='chat'>  <body>test msg</body>  <thread>connect29243</thread>  <html xmlns='http://jabber.org/protocol/xhtml-im'>    <body xmlns='http://www.w3.org/1999/xhtml'>      <font color="red">test msg</font>    </body>  </html>  <active xmlns='http://jabber.org/protocol/chatstates'/></message>

需要注意的实现细节

发送消息的时候,需要将纯文本放在body中,同时将带格式的消息放在html/body中。需要注意的是,Message中不能包含<, >, &, ', " 5个特殊的字符,在发送给server之前需要将他们做转换(&lt;, &gt;, &amp;, &apos;, and &quot;),同时对于&nbsp;也需要做特殊的处理。
对于带格式消息中的<font>, </font>等,我们需要保持原样,否则消息接受者就无法正确的显示消息。

当client接受到一个消息,首先要尝试获取xhtml中的格式消息,如果获取不到,才获取body中的纯文本消息。由于body中只能放普通的文本消息,因此对于hyperlink消息,由于上层给SDK的是类似于<a href="www.sina.com"> sina </a>这样的格式,因此我们需要将这个hyperlink替换为www.sina.com放在body中。

原创粉丝点击