0

我正在尝试构建一个简单的 Jabber 客户端。我已经下载了这个示例项目,它使用xmpp-framework https://github.com/funkyboy/Building-a-Jabber-client-for-iOS 我在 iOS 模拟器中运行它。我已经在本地安装了 Openfire,以便我可以与登录 iChat 的用户进行交互。

不幸的是,该应用程序只接收消息。它无法发送消息并给出错误“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:@"user@server.local"];

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

有人可以帮忙吗?谢谢你。

4

1 回答 1

1

没有理由将 TURN 用于正常消息传递。只有媒体流需要 TURN。只需使用XMPPFramework。有一些很好的入门指南。

接下来,使用这种性质的代码来创建和发送节:

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

请注意,这msg只是 NSXMLElement 的子类,因此您可以随意修改 XML 来制作您要发送的协议。

于 2011-11-22T23:14:42.243 回答