Disconnection of XMPP client while sending big size image, video and audio

那年仲夏 提交于 2019-12-02 09:16:08

While XMPP allow to transfer small amount of binary data, it is recommended to share large files or video/audio streams "out of band" and use XMPP as signaling protocol - you should just send http:// or rtp:// link, and your buddy's client will download it, or start playing audio/video from given stream. You can send that link in any form, but there are some "draft standard" XMPP extensions, which allow to get compatibility with existing clients:

  1. XEP-0066: Out Of Band Data - simple extension, where you just attach URL to standard <message /> element.
  2. XEP-0095: Stream Initiation and XEP-0166: Jingle - these are "meta"-specifications, describing how clients are "negotiate" their supported features, selects which way to share data their prefer (ibb, oob, bytestreams, jingle rtp, etc.), finds opened TCP ports or mediating proxies, NAT traversal, etc.

So, if you just want to share file - share it via any existing file sharing service and send URL as described in the first XEP. If you want to build large service or VOIP application - use SI or Jingle. But no one stop you to create your own XMPP extension, which may be simpler to implement. Here is a custom XMPP extension, which was developed not by XMPP community, but accepted by XSF as experimental, you can check it too.

Even I am not sending the image directly to xmpp because it takes a lot time.

In my project I message can be string and it can be image and I found there is only one method to send message i.e..

[xmppRoom sendMessageWithBody:@"Your msg"];

So for sending the image First I am converting it to base64 then uploading it to my server and getting the URL for that image from server. Once we get the URL just pass that URL in Above method.

Problem here I was facing was how to segregate normal messages and URL (Images)

So for sending normal text I am directly sending it above function but for sending URL I am sending the nsdictionary i.e. I am converting nsdictionary to string and sending it in above function

NSDictionary *dict = @{@"type":@"image",
                               @"url":@"Url of your image"};

            NSString *newMessage = [NSString stringWithFormat:@"%@",dict];

            [appDelegate.xmppRoom sendMessageWithBody:newMessage];

For segregating normal message and Image in -

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{

    messages *msg = [messages new];

     NSDictionary *dictOfMedia = [NSPropertyListSerialization
                                 propertyListWithData:[[[message elementForName:@"body"] stringValue] dataUsingEncoding:NSUTF8StringEncoding]
                                 options:kNilOptions
                                 format:NULL
                                 error:NULL];

    if ([dictOfMedia objectForKey:@"type"])
    {
        msg.mediaType = [dictOfMedia objectForKey:@"type"];
        msg.url = [dictOfMedia objectForKey:@"url"];
    }
    else
    {
        msg.msg = [[message elementForName:@"body"] stringValue];
    }


}

messages is my model class you can use simply use nsstring for testing purpose.

After this just use any open source project for downloading the image or perform lazy loading on your own.

Hope this will help you :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!