XMPP协议发送聊天消息,图片,文件

来源:互联网 发布:淘宝五个金冠 编辑:程序博客网 时间:2024/06/10 06:08

首先导入头文件

/** *聊天消息模块 */@property (nonatomic, strong) XMPPMessageArchiving *msgArc;/** *聊天消息存储模块 */@property (nonatomic, strong) XMPPMessageArchivingCoreDataStorage *msgStorage;

设置聊天消息模块,并激活

self.msgStorage=[[XMPPMessageArchivingCoreDataStorage alloc]init];        self.msgArc=[[XMPPMessageArchiving alloc]initWithMessageArchivingStorage:self.msgStorage];        [self.msgArc activate:self.stream];

方案1

//text 纯文本

//image 图片

//sound 音频

//doc word文件

//xls excel表格

 <message type="chat" to="liusong@127.0.0.1" bodyType="image">

    <body>image</body>

    <attachment>xxxx</attachment>//xxxx放文件装成的字符串

 </message>

-(void)sendMessageWithType:(NSString*)type data:(NSData *)data{    XMPPMessage *message=[XMPPMessage messageWithType:@"chat" to:self.friendJid];    [message addAttributeWithName:@"body-type" stringValue:type];    NSString *base64Str=[data base64EncodedStringWithOptions:0];    XMPPElement *element=[XMPPElement elementWithName:@"attachment" stringValue:base64Str];    [message addChild:element];    [message addBody:type];    XMPPTool *tool=[XMPPTool sharedXMPPTool];    [tool.stream sendElement:message];}
但是用上述方法会造成xml携带的数据量太大,对openfire服务器造成的压力也太大,所以一般比较大的公司多会有文件服务器,首先把文件上传到文件服务器,然后文件服务器把存储路径返回来,然后在把文件路径上传到openfire服务器,上传文件用put方法,好处是上传路径即是保存路径,为了不让路径重复,这里使用时间具体到秒然后在加上用户名,而不管上传什么图片还是语音,文件都是NSData所以只需要把相应的数据变成data即可,为了区别加上bodyType来判断

-(void)sendMessageWithType:(NSString*)type data:(NSData *)data{    // 1.把文件上传到文件服务器    /*     * > 文件上传的路径     *   http://localhost:8080/imfileserver/Upload/Image/ + 文件名     http://localhost:8080/imfileserver/Upload/Image/bcq     http://localhost:8080/imfileserver/Upload/Image/xyy          * > 文件上传的方法不是使用POST,而是使用put,在公司开中,put方式的文件上传比较常用     put: 文件上传的路径也就是文件下载的路径     */        // 1.1定义文件名 user + 20151107201112(liusong20151107201112)    NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];    dateForm.dateFormat = @"yyyyMMddHHmmss";    NSString *currentTimeStr = [dateForm stringFromDate:[NSDate date]];    NSString *fileName = [[LSAccount sharedAccount].loginName stringByAppendingString:currentTimeStr];        // 1.2拼接文件上传路径    NSString *uploadPath = [@"http://localhost:8080/imfileserver/Upload/Image/" stringByAppendingString:fileName];        // 2.上传成功后,把文件路径发送给Openfire服务器    HttpTool *httpTool = [[HttpTool alloc] init];    // 图片上传的时候,以jpg格式上传    // 因为文件服务器我只接收jpg    [httpTool uploadData:data url:[NSURL URLWithString:uploadPath] progressBlock:nil completion:^(NSError *error) {        if (!error) {            XMPPMessage *msg = [XMPPMessage messageWithType:@"chat" to:self.friendJid];            [msg addAttributeWithName:@"bodyType" stringValue:type];            [msg addBody:uploadPath];            [[XMPPTool sharedXMPPTool].stream sendElement:msg];        }else{            LSLog(@"上传失败");        }    }];}
</pre><pre name="code" class="objc">
</pre><p></p><p>数据显示时进行判断</p><p><pre name="code" class="objc">-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static  NSString *ID=@"cell";    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];    if (cell==nil) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];    }    XMPPMessageArchiving_Message_CoreDataObject *msg=self.resultController.fetchedObjects[indexPath.row];    // 判断消息的类型到有没有附件    // 1.获取原始的xml数据    XMPPMessage *message =  msg.message;        // 获取附件的类型    NSString *bodyType = [message attributeStringValueForName:@"bodyType"];        if ([bodyType isEqualToString:@"image"]) {//图片        // 获取文件路径        NSString *url = message.body;        //显示图片        [cell.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"]];                // 清除循环引用导致的残留文字        cell.textLabel.text = nil;    }else if([bodyType isEqualToString:@"sound"]){        //做语音相应的处理    }else if([bodyType isEqualToString:@"doc"]){        //做相应的处理    }    else{//纯文本        cell.textLabel.text = msg.body;        // 清除循环引用导致的残留图片        cell.imageView.image = nil;    }    return cell;}




0 0