2011-11-22 49 views
0

我想構建一個簡單的Jabber客戶端。 我已經下載了這個示例項目,它使用xmpp框架https://github.com/funkyboy/Building-a-Jabber-client-for-iOS 我正在iOS模擬器中運行它。我在本地安裝了Openfire,以便我可以與登錄到iChat的用戶進行交互。iPhone Jabber/XMPP客戶端...「TURN連接失敗」

不幸的是,該應用程序只有收到消息。它發送錯誤消息「TURN Connection failed!」失敗。

這是代碼嘗試連接:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.tView.delegate = self; 
    self.tView.dataSource = self; 
    [self.tView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 

    messages = [[NSMutableArray alloc ] init]; 

    JabberClientAppDelegate *del = [self appDelegate]; 
    del._messageDelegate = self; 

    [self.messageField becomeFirstResponder]; 

    XMPPJID *jid = [XMPPJID jidWithString:@"[email protected]"]; 

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

    TURNSocket *turnSocket = [[TURNSocket alloc] initWithStream:[self xmppStream] toJID:jid]; 

    [turnSockets addObject:turnSocket]; 

    [turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()]; 
    [turnSocket release]; 
} 

而且那些被稱爲成功/失敗的方法:

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

任何人都可以請幫助? 謝謝。

回答

1

沒有理由使用TURN進行正常消息傳遞。 TURN僅適用於媒體流。只需使用XMPPFramework。有一些很好的入門指南。

下,這種性質的使用代碼來創建和發送節:

XMPPMessage *msg = [XMPPMessage message]; 
[msg addAttributeWithName:@"type" stringValue:@"chat"]; 
[msg addAttributeWithName:@"to" stringValue:@"[email protected]"]; 
NSXMLElement *body = [NSXMLElement elementWithName:@"body" stringValue:@"Hello"]; 
[msg addChild:body]; 
[[self xmppStream] sendElement:msg]; 

注意msg只是NSXMLElement的子類,這樣你就可以隨意修改XML手藝你要去協議發送。

+0

請您指出一些問題嗎?我只找到了關於如何啓動連接的文檔 - 無論如何發送消息。非常感謝你。 –

+0

結果不需要TURN - 錯誤與無法發送消息無關。我將sendMessage方法移至App Delegate,並開始工作。我不知道爲什麼。但它的工作。 –

+0

@Joe: - 您能否介紹一下iOS中的集成XMPP,任何鏈接或文檔?我對此很失敗。 – iOSAppDev