XMPP Sending/Receving file in iphone sdk …?

2019-01-14 04:53发布

问题:

How to send/receive file in iphone sdk using xmpp framework ? Currently i am using XEP-0065 classes, and use the following code:

ObjTURNSocket = [[TURNSocket alloc] initWithStream:((TestAppDelegate*)[[UIApplication sharedApplication] delegate]).xmppStream 
                                                 toJID:chatuser.jid];
    [ObjTURNSocket start:self];

And I am getting following response from the server:

<iq type="error" id="AB2ED567-B97F-4DFE-B789-7731A617C239" to="kapil@testweb/6df6dc96" from="jabber.org">
   <query xmlns="http://jabber.org/protocol/disco#items"/>
   <error code="404" type="cancel">
      <remote-server-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
   </error>
 </iq>`

Any help or suggestion will be well appreciated.

回答1:

First, call setProxyCandidates with an array of the possible servers that you might be able to use for proxying. The default is "jabber.org", and your server is not federated with jabber.org, which is why you are getting the disco error back.

Next, in your delegate, implement the following methods:

- (void)turnSocket:(TURNSocket *)sender didSucceed:(GCDAsyncSocket *)socket;
- (void)turnSocketDidFail:(TURNSocket *)sender;

Then, in your didSucceed implementation, send data on the socket that was passed in, using:

- (void)writeData:(NSData *)data 
      withTimeout:(NSTimeInterval)timeout 
              tag:(long)tag;

Finally, close the socket:

- (void)disconnectAfterWriting;

I figured this out by looking at the source for TURNSocket.m, looking for the hardcoded "jabber.org", then searching for [delegate to find the places the delegate was getting called. This took me back to TURNSocket.h, which had a protocol documented for the delegate to implement.



回答2:

I ended up having to customize the TURNSocket class to meet my specific needs to be able to transfer a file from my iOS device to another device. If there is a proxy server available, then the TURNSocket class might work for one's needs. But if this is a direct connection where a proxy server may not be available, then some extra work is necessary to set up your device to be able to connect to another device and directly transfer a file.

I was able to receive a file using TURNSocket in its current form with only one minor modification. As the code currently stands, the id and sid are assigned the same value, which cannot be guaranteed that a received stanza will have the same unique identifier value for both the id and sid.



回答3:

You should have use xep-96 to make it possible to share and receive files. after that just initiate xmppSifiletranfer with relevant data. like

-(void)sendToOtherDevice:(NSData *)fileData receiverJid:(XmPPJId *)senderFullID file:(NSString *)fileName{

            myFileTransferID=[xmppStream generateUUID];
            XMPPJID *jid =senderFullID;
            sifiletransfer=[[XMPPSIFileTransfer alloc]init];
            [sifiletransfer initiateFileTransferTo:jid withData:fileData file:fileName passedsid:myFileTransferID];

            if ([jid.domain isEqualToString:[xmppStream myJID].domain]) {
                [TURNSocket setProxyCandidates:[NSArray arrayWithObjects:jid.domain, nil]];
            } else {
                [TURNSocket setProxyCandidates:[NSArray arrayWithObjects:jid.domain,[xmppStream myJID].domain, nil]];
            }
             TURNSocket *socket = [[TURNSocket alloc] initWithStream:xmppStream toJID:jid sid:myFileTransferID];
           // [socket setDataToSend:fileData];

            [socket startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    }
    # delegater of turnsocket
    - (void)turnSocket:(TURNSocket *)sender didSucceed:(GCDAsyncSocket *)socket
    {
        NSLog(@"Socket Suceeed Port For File Transfer: %d",socket.localPort);
        DDLogInfo(@"TURN Connection succeeded!");
        DDLogInfo(@"You now have a socket that you can use to send/receive data to/from the other person.");
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hurray!!"
         message:@"Conection Established"
         delegate:nil
         cancelButtonTitle:@"Ok"
         otherButtonTitles:nil];
         [alertView show];
    }

if you guys have any other issue regarding file transfer comment below.I will surely help you.