Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xmpp - send/receive file using turnsocket in ios

I'm working on chat app. Now I want to send a file using TurnSocket class but socket failed to connect.

- (void)sendToOtherDevice:(NSData *)fileData receiverJid:(NSString *)receiverJid {

    XMPPJID *JID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@/%@",receiverJid,resourceID]];

    NSLog(@"%@",[JID full]);

    NSLog(@"Attempting TURN connection to %@", JID);

    [TURNSocket setProxyCandidates:[NSArray arrayWithObjects:JID.domain, nil]];
    TURNSocket *turnSocket = [[TURNSocket alloc] initWithStream:[self xmppStream] toJID:JID];
    [turnSockets addObject:turnSocket];
    [turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
}

TurnSocket Delegate Method

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

    NSLog(@"TURN Connection succeeded!");
    NSLog(@"You now have a socket that you can use to send/receive data to/from the other person.");

    [turnSockets removeObject:sender];
}

- (void)turnSocketDidFail:(TURNSocket *)sender {

    NSLog(@"TURN Connection failed!");
    [turnSockets removeObject:sender];

}

Log:

SEND:

<iq type="get" to="meinantrieb.net" id="C6CF8EEE-B74E-45C8-8881-89BD0835056E">
<query xmlns="http://jabber.org/protocol/disco#items"/></iq>

RECV:

<iq xmlns="jabber:client" from="meinantrieb.net" to="[email protected]/267698820914007696033596" id="C6CF8EEE-B74E-45C8-8881-89BD0835056E" type="result">
<query xmlns="http://jabber.org/protocol/disco#items">
<item jid="conference.meinantrieb.net"/>
<item jid="irc.meinantrieb.net"/>
<item jid="proxy.meinantrieb.net"/>
<item jid="pubsub.meinantrieb.net"/>
<item jid="vjud.meinantrieb.net"/></query></iq>

SEND:

<iq type="get" to="proxy.meinantrieb.net" id="22D98197-F1F2-4D8F-AA9C-E1076D0EF9F7">
<query xmlns="http://jabber.org/protocol/disco#info"/></iq>

RECV:

<iq xmlns="jabber:client" from="proxy.meinantrieb.net" to="[email protected]/267698820914007696033596" id="22D98197-F1F2-4D8F-AA9C-E1076D0EF9F7" type="result">
<query xmlns="http://jabber.org/protocol/disco#info">
<identity category="proxy" type="bytestreams" name="SOCKS5 Bytestreams"/>
<feature var="http://jabber.org/protocol/disco#info"/>
<feature var="vcard-temp"/>
<feature var="http://jabber.org/protocol/bytestreams"/>
<x xmlns="jabber:x:data" type="result">
<field var="FORM_TYPE" type="hidden">
<value>http://jabber.org/network/serverinfo</value></field></x></query></iq>

SEND:

<iq type="get" to="proxy.meinantrieb.net" id="84AFA217-7F71-48B4-B56C-66995FE20490">
<query xmlns="http://jabber.org/protocol/bytestreams"/></iq>

RECV:

<iq xmlns="jabber:client" from="proxy.meinantrieb.net" to="[email protected]/267698820914007696033596" id="84AFA217-7F71-48B4-B56C-66995FE20490" type="result">
<query xmlns="http://jabber.org/protocol/bytestreams">
<streamhost jid="proxy.meinantrieb.net" host="0.0.0.0" port="7777"/></query></iq>

SEND:

<iq type="set" to="[email protected]/(null)" id="694B5F07-3A70-4556-9268-1D47AC251229">
<si xmlns="http://jabber.org/protocol/si" id="694B5F07-3A70-4556-9268-1D47AC251229" profile="http://jabber.org/protocol/si/profile/file-transfer">
<file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="abc.txt" size="2239350"/>
<feature xmlns="http://jabber.org/protocol/feature-neg">
<x xmlns="jabber:x:data" type="form">
<field var="stream-method" type="list-single"><option><value>http://jabber.org/protocol/bytestreams</value></option><option><value>http://jabber.org/protocol/ibb</value></option></field></x></feature></si></iq>

RECV:

<iq xmlns="jabber:client" from="[email protected]/(null)" to="[email protected]/267698820914007696033596" type="result" id="694B5F07-3A70-4556-9268-1D47AC251229">
<si xmlns="http://jabber.org/protocol/si">
<feature xmlns="http://jabber.org/protocol/feature-neg">
<x xmlns="jabber:x:data" type="submit">
<field var="stream-method"><value>http://jabber.org/protocol/bytestreams</value></field></x></feature></si></iq>

SEND:

<iq type="set" to="[email protected]/(null)" id="694B5F07-3A70-4556-9268-1D47AC251229"><query xmlns="http://jabber.org/protocol/bytestreams" sid="694B5F07-3A70-4556-9268-1D47AC251229" mode="tcp">
<streamhost xmlns="http://jabber.org/protocol/bytestreams" jid="proxy.meinantrieb.net" host="0.0.0.0" port="7777"/></query></iq>

RECV:

<iq xmlns="jabber:client" from="[email protected]/(null)" to="[email protected]/267698820914007696033596" type="error" id="694B5F07-3A70-4556-9268-1D47AC251229">
<error code="404" type="cancel">
<item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>

TURN Connection failed!

Any help or suggestion will be well appreciated.

like image 232
Kirit Vaghela Avatar asked Nov 01 '22 00:11

Kirit Vaghela


1 Answers

it's better to focus on getting two jabber clients talking first as there are many issues in communicating to other clients as far as i know. (i.e. Adium, Spark)
To establish a connection between two jabber clients is now working. There are indeed some issues to connect to other jabber clients like Adium or Spark.

For two jabber clients to connect, one has to be the server (target) and one has to be the client (requester).

Therefore, we need to implement the incoming request from another client. To achieve this, we need fews lines of code for incoming TURN request.

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
    DDLogVerbose(@"%@: %@ - %@", THIS_FILE, THIS_METHOD, [iq elementID]);

    if ([TURNSocket isNewStartTURNRequest:iq]) {
        NSLog(@"IS NEW TURN request..");
        TURNSocket *turnSocket = [[TURNSocket alloc] initWithStream:[self xmppStream] incomingTURNRequest:iq];
        [turnSockets addObject:turnSocket];
        [turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
        [turnSocket release];
    }

    return YES;
}

if you test on two jabber clients, you'd be able to establish a connection from either side. Receiving data from Spark is fine though sending data still got the 406 error. Cant communicate with Adium in either way.
My suggestion:

  1. Try a dedicated server on your own. (I am using the VPS hosting by Linode)
  2. Obtain your jid from primaryResource.jid
like image 169
Jerin Avatar answered Nov 15 '22 05:11

Jerin