xmpp - 使用ios中的turnsocket发送/接收文件

时间:2014-05-07 12:04:33

标签: ios xmppframework

我正在使用聊天应用。现在我想使用TurnSocket类发送文件,但套接字无法连接。

- (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委派方法

- (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];

}

日志:

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="kirit@meinantrieb.net/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="kirit@meinantrieb.net/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>

发送:

<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="kirit@meinantrieb.net/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>

发送:

<iq type="set" to="vote@meinantrieb.net/(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="vote@meinantrieb.net/(null)" to="kirit@meinantrieb.net/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="vote@meinantrieb.net/(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="vote@meinantrieb.net/(null)" to="kirit@meinantrieb.net/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连接失败!

任何帮助或建议都将受到赞赏。

2 个答案:

答案 0 :(得分:2)

最好是专注于让两个jabber客户先说话,因为据我所知,与其他客户沟通有很多问题。 (即Adium,Spark)
为了在两个jabber客户之间建立连接,现在正在运作。确实有一些问题需要连接到其他jabber客户端,如Adium或Spark。

对于两个连接的jabber客户端,一个必须是服务器(目标),一个必须是客户端(请求者)。

因此,我们需要实现来自其他客户端的传入请求。为实现这一目标,我们需要几行代码来传入TURN请求。

- (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;
}

如果您在两个jabber客户端上进行测试,您就可以从任何一方建立连接。从Spark接收数据很好,但发送数据仍然出现406错误。 不能以任何一种方式与Adium沟通 我的建议是:

  1. 自行尝试使用专用服务器。 (我使用的是Linode的VPS主机)
  2. 从primaryResource.jid获取您的jid

答案 1 :(得分:0)

在xmppStream上使用turnsocket发送iq时,请确保使用正确的jid和来自jid。

像这样。 致:arpan@fb.purologics.com/hsdvydvdv
来自:push@fb.purologics.com/ksjbdjdd 之后&#39; /&#39; &#34; hsdvydvdv&#34;和&#34; ksjbdjdd&#34;是资源id。发送iq以进行正确连接时必须使用。

你可以在以下AppDelegate方法中获取完整的jid:

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
    DDLogVerbose(@"%@: %@ - %@", THIS_FILE, THIS_METHOD, [presence fromStr]);

  //  NSString *presenceType = [presence type]; // online/offline
    NSString *myUsername = [[sender myJID] user];
    NSString *presenceFromUser = [[presence from] user];

    NSLog(@" Printing full jid of user %@",[[sender myJID] full]);
    NSLog(@"Printing full jid of user %@",[[sender myJID] resource]);
    NSLog(@"From user %@",[[presence from] full]);
    senderFullID=[[presence from] full];



}