stomp webSocket kurento xmppiOS程序猿

XMPP - 自定义 </message> 节点

2017-02-06  本文已影响289人  LonlyCat

上一篇内容中我们已经提到 XMPP 消息的传递就是 XML 串的传递,而用户之间聊天内容是处于 </Message> 节点中。

// 这就是一个最基本的 </Message> 节点
<message from='darkcave@chat.shakespeare.lit/firstwitch'
         to='hecate@shakespeare.lit/broom'
         type='groupchat'>
  <body>Thrice the brinded cat hath mew'd.</body>
</message>

那么自定义 </Message> 节点有什么用呢?
IM 发展到现在 语音图片 这类二进制文件,甚至还有一些 应用内分享 都逐渐成为标配功能,而要实现这些功能我们就需要 自定义 </Message> 节点 。

如何自定义 </Message> 节点

刚刚讲到自定义 </Message> 节点的目的就是我们要实现一些定制功能。那么为了区分这些功能我们就自己定义一个类型放入 </Message> 节点中。

// 常见的几处放入自定义节点的位置
1、</Message> 的属性中
<message from='darkcave@chat.shakespeare.lit/firstwitch'
         to='hecate@shakespeare.lit/broom'
         type='groupchat'
         customtype='customtype'>
  <body>Thrice the brinded cat</body>
</message>

2、</Message> 的子节点中
<message from='darkcave@chat.shakespeare.lit/firstwitch'
         to='hecate@shakespeare.lit/broom'
         type='groupchat'>
  <body>Thrice the brinded cat</customtype>
  <customtype>customtype</customtype>
</message>

3、</Message> 下的 </body> 节点中
<message from='darkcave@chat.shakespeare.lit/firstwitch'
         to='hecate@shakespeare.lit/broom'
         type='groupchat'>
  <body> customtype | Thrice the brinded cat</body>
</message>

应用内分享

其实懂得在 </Message> 节点的什么位置插入类型字段,应用内分享就不是问题了,比如我想发送一个音乐类型的分享消息。

<message from='darkcave@chat.shakespeare.lit/firstwitch'
         to='hecate@shakespeare.lit/broom'
         type='groupchat'>
  <body>id | title | singer | second</body>
  <customtype>music</customtype>
</message>

这样我们就可以根据 </customtype> 取出的字符串知道我们分享的内容是音乐类型,再从 </body> 中按规则解析我们所需要的内容并展示在 UI 上。

二进制文件分享

二进制文件分享比起应用内分享就稍微复杂一些。大体有两总方式:

使用普通消息类型发送
优点:不需要对方在线,可通过离线消息获取
缺点:文件大小限制大

使用 XEP - 0096 文件传输协议发送
优点:文件大小基本无限制,但大文件需要分次发送
缺点:必须得对方在线

两总方式总的来说思路是一样的:把文件进行 base64 编码转成字符串拼接到 XML 中。我们这里暂时只讨论 语音图片 两总常见文件( 使用第一种方式 ),XEP - 0096 协议以后有空再细说。

语音消息

iOS 下的录音基本为以下两种方式:

但是这俩种方式的音频文件都有一个共同的问题——压缩率低。所以 我们就需要在 base64 编码之前将音频文件转成一个压缩率高的格式。

现在比较流行的高压缩比格式有俩种 MP3AMR ,这里因为是聊天不需要特别高的音质,个人推荐使用 AMR

如果你需要录音并转成 MP3 格式,推荐 iOSMp3Recorder
如果你需要录音并转成 AMR 格式,推荐 VoiceConvert

// 使用上面俩个开源的工具,我们可以很方便的得到录音数据,通过代理调用下面这个方法
- (void)needSendVoice:(NSData *)voice time:(NSInteger)second
{   
    if (voice) {
        
        // 这里是实现了一个工具类快速生成 XMPPMessage 的 Body
        NSString  *message = [XMPPManagerHelper voiceMessageWithSecond:second];
        // base64 编码
        NSString  *encodeData = [voice base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        // 发送消息
        [self sendFileMessage:message withEncodeData:encodeData];
    }
}

// 拼接并发送消息
- (void)sendFileMessage:(NSString *)message withEncodeData:(NSString *)encodeData
{
    NSString      *type = _isGroupChat ? @"groupchat" : @"chat";
    // 这里自定义了一个 file 节点, 将编码完成后的字符串放入 file 节点中
    DDXMLElement  *file = [DDXMLElement elementWithName:@"file" stringValue:encodeData];
    
    __weak typeof(self) weakSelf = self;
    __block IMMessageModel *model = [getXMPPManager() sendMessage:message
                                                               to:_jid
                                                             type:type
                                                           extend:file
                                                     statusHandle:^(BOOL status)
                                     {
                                         
                                         NSLog(@"收到 Message 发送回调 : %@", @(status));
                                         if (status) {
                                             
                                             model.status = IMMessageStatusCompleted;
                                         } else {
                                             
                                             model.status = IMMessageStatusFailed;
                                         }
                                         [model update];
                                         [weakSelf loadChatDatas];
                                     }];
    
    [_chatDatas addObject:model];
    [_tableView reloadData];
    [self scrollToEndMessage];
}

最后发出的消息格式如下:

<message from='darkcave@chat.shakespeare.lit/firstwitch'
         to='hecate@shakespeare.lit/broom'
         type='groupchat'
         id='ea430-deg'>
  <body>语音</body>
  <file>CZ63hd+MKX+e2ZhB2RzaXanVVVaYgQIoIXneouFGIYNtFh1QnhllldBiCyC7SkVvy+YiiEXmOKRJsG2ShTbCeBPvH8Y4IROEAv7pzyECiXd8QMkpjQGKTkFqMirbz/cq5wPE5+F7/m/Xs</file>
  <customtype>Audio</customtype>
</message>

图片消息

图片消息与语音消息类似,只需要修改下类型就好

<message from='darkcave@chat.shakespeare.lit/firstwitch'
         to='hecate@shakespeare.lit/broom'
         type='groupchat'
         id='ea430-det'>
  <body>图片</body>
  <file>CZ63hd+MKX+e2ZhB2RzaXanVVVaYgQIoIXneouFGIYNtFh1QnhllldBiCyC7SkVvy+YiiEXmOKRJsG2ShTbCeBPvH8Y4IROEAv7pzyECiXd8QMkpjQGKTkFqMirbz/cq5wPE5+F7/m/Xs</file>
  <customtype>Photo</customtype>
</message>

但针对图片来说,现在大部分 IM 的做法是上传高清图在自己服务器,同时从 XMPP 发送一张略缩图给对方,这样可以保证对方消息接收的速度,也可以留存清晰的图片。下面给出一个生成略缩图的方法。

- (UIImage *)imageCompressScale:(float)scale
{
    CGSize size = self.size;
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGFloat scaledWidth = width * scale;
    CGFloat scaledHeight = height * scale;
    UIGraphicsBeginImageContext(size); // this will crop
    [self drawInRect:CGRectMake(0, 0, scaledWidth, scaledHeight)];
    UIImage* newImage= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
上一篇下一篇

猜你喜欢

热点阅读