我正在iPhone中使用XMPP Framework创建聊天应用程序。我想知道发送和接收消息的过程。任何人都可以给我一个解决方案吗?
提前致谢。
答案 0 :(得分:10)
下载XMPPFramework并解压缩。里面有几个文件夹。打开'Xcode'文件夹>打开'iPhoneXMPP'文件夹>点击“iPhoneXMPP.xcodeproj”>运行。它首先要求登录凭证。成功登录后,它将显示您的好友列表。它适用于Gmail。每个传入消息都有一个回调方法:
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
user = [xmppRosterStorage userForJID:[message from] xmppStream:sender managedObjectContext:[self managedObjectContext_roster]];
if ([message isChatMessageWithBody])
{
NSString *body = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:body forKey:@"msg"];
[m setObject:from forKey:@"sender"];
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
NSLog(@"Applications are in active state");
//send the above dictionary where ever you want
}
else
{
NSLog(@"Applications are in Inactive state");
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertAction = @"Ok";
localNotification.applicationIconBadgeNumber=count;
localNotification.alertBody =[NSString stringWithFormat:@"From:"%@\n\n%@",from,body];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
//send the above dictionary where ever you want
}
}
}
对于发送消息,我们必须在任何地方编写我们自己的方法:
-(void)sendMessage
{
NSString *messageStr =messageField.text;
if([messageStr length] > 0)
{
NSLog(@"Message sending fron Gmail");
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:messageStr];
NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:@"destination address"];
[message addChild:body];
NSLog(@"message1%@",message);
[[self appDelegate].xmppSream sendElement:message];
}
}
答案 1 :(得分:2)
对于在组中发送消息/下面的房间是片段
XMPPMessage *message = [XMPPMessage message];
[message addBody:@"123"];
[self.currentRoom sendMessage:message1];
Where self.currentRoom is XMPPRoom
答案 2 :(得分:1)
如果您要从Room/Group
发送消息,请使用此代码发送消息。
[xmppRoom sendMessage:@"Hi All"];
不需要通过xmppStream
发送消息。单行代码对我来说非常适合。
答案 3 :(得分:0)
快速谷歌搜索显示许多XMPP libraries,无论是C / C ++还是ObjC。也许http://code.google.com/p/xmppframework/可能是一个很好的起点,虽然我没有亲自尝试过。
答案 4 :(得分:0)
以下是通过Swift 3中的XMPPFramework发送消息的解决方案
let user = XMPPJID(string: "user@jabjab.de")
let msg = XMPPMessage(type: "chat", to: user)
msg?.addBody("Message to send")
self.xmppStream.send(msg)