4

我正在使用 iPhone 中的 XMPP 框架创建一个聊天应用程序。我想知道发送和接收消息的过程。谁能给我一个解决方案?

提前致谢。

4

5 回答 5

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];
    }    
}
于 2012-10-10T13:00:49.250 回答
2

下面是在组/房间中发送消息的片段

XMPPMessage *message = [XMPPMessage message];
[message addBody:@"123"];
[self.currentRoom sendMessage:message1]; 

Where self.currentRoom is XMPPRoom
于 2014-10-06T13:41:56.547 回答
1

如果您从中发送消息,请Room/Group使用此代码发送消息。

[xmppRoom sendMessage:@"Hi All"];

不需要通过xmppStream. 这行代码非常适合我。

于 2014-05-14T13:47:33.473 回答
0

快速谷歌搜索显示许多XMPP 库,无论是 C/C++ 还是 ObjC。也许http://code.google.com/p/xmppframework/将是一个很好的起点,尽管我没有亲自尝试过。

于 2011-02-28T07:33:15.583 回答
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)
于 2017-07-19T10:14:55.910 回答