I have implemented xmpp using robbiehanson xmpp example. I am able to chat and also able to send images. The images which i am sending are converted to nsdata and further converted to Base64String, and later sending the string with
[self.xmppStream sendElement:message];
This way if the size of image is small i am able to send that instantly but if the size of images is bigger the two xmpp user gets disconnected and the file is not transferred. Same thing happens with video and audio. Though i am able to compress the images using
UIImageJPEGRepresentation(image,0.005);
But how to send the video and audio as the size would obviously be big. I have read documents on XEP - 0065 and XEP - 0096 but not able to implement it in a proper way. Please let me know why do the users gets disconnected, why i am not able to transfer heavy files using base64 and also share the code which would help me to transfer video and audio.
Would really appreciate you help.
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:
- XEP-0066: Out Of Band Data - simple extension, where you just attach URL to standard
<message />
element. - 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 :)
来源:https://stackoverflow.com/questions/22803896/disconnection-of-xmpp-client-while-sending-big-size-image-video-and-audio