iPhone Jabber/XMPP client... "TURN Connection failed"
I am trying to build a simple Jabber client. I have downloaded this sample project, which uses xmpp-framework https://github.com/funkyboy/Building-a-Jabber-client-for-iOS I am running it in the iOS Simulator. I have installed Openfire locally so that I can interact with a user logged into iChat.
Unfortunately the app only receives messages. It fails in sending messages giving the error "TURN Connection failed!".
This is the code attempting to connect:
- (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];
}
And those are the methods called on success/failure:
- (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];
}
Can anyone please help? Thank you.
There is no reason to use TURN for normal messaging. TURN is required for media streaming only. Just use XMPPFramework. There are some good getting-started guides.
Next, use code of this nature to create and send stanzas:
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];
Note that msg
is just a subclass of NSXMLElement, so you can modify the XML at will to craft the protocol you're going to send.