我正在开发一个利用Robbie Hanson的XMPPFramework的iOS XMPP聊天应用程序。
实现了最重要的功能--发送和接收消息。基本上,我已经构建了一个基本的功能性聊天应用程序,当然还有一些花哨的东西。
现在,我的问题是关于MUC。我在其他网站上看到的代码显示,在XMPPRoom中有一个方法initWithRoomName。然而,在我克隆的git代码库中没有这种方法。那么,有什么替代方案呢?或者,如果没有,我该如何使用XMPPFramework创建房间?
谢谢。
发布于 2012-06-09 16:05:02
下面是我是如何解决自己的问题的。请注意,此解决方案根本不涉及XMPPRoom。首先,我创建了一个方法,根据情况创建或进入房间。(根据XMPP文档,创建的XML请求与您进入房间时发送的XML请求相同;也就是说,如果您进入房间时房间还不存在,服务将为您创建它。)
我们开始吧。这是创建/进入房间的方法。此方法的作用是向您想要创建/进入的房间发送一个存在。如果您是第一个进入房间,但房间尚未创建,则您将自动成为房间的所有者和主持人。
- (void)createOrEnterRoom:(NSString *)roomName
{
//here we enter a room, or if the room does not yet exist, this method creates it
//per XMPP documentation: "If the room does not yet exist, the service SHOULD create the room"
//this method accepts an argument which is what you would baptize the room you wish created
NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
NSString *room = [roomName stringByAppendingString:@"@conference.jabber.com/iMac"];
[presence addAttributeWithName:@"to" stringValue:room];
NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"http://jabber.org/protocol/muc"];
NSXMLElement *history = [NSXMLElement elementWithName:@"history"];
[history addAttributeWithName:@"maxstanzas" stringValue:@"50"];
[x addChild:history];
[presence addChild:x];
[[self xmppStream] sendElement:presence];
}接下来,在声明了XMPPStream方法的AppDelegate中,我们通过检查服务器发送的状态码来过滤在didReceivePresence方法中收到的响应。如果状态码是201,答对了!房间的创建进行得很顺利。除201以外的状态码有不同的含义,但出于我们的目的,让我们重点关注201。
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
NSXMLElement *x = [presence elementForName:@"x" xmlns:@"http://jabber.org/protocol/muc#user"];
for (NSXMLElement *status in [x elementsForName:@"status"])
{
switch ([status attributeIntValueForName:@"code"])
{
case 201: [self notifyRoomCreationOk:room];
}
}
}然后,我们告诉服务器我们正在创建一个"instant“类型的房间,这意味着我们将发送一个IQ元素来告诉它房间默认值。notifyRoomCreationOk是一个委托方法,当房间创建成功时,在不同的视图中调用,毕竟我必须在文本文件中记录房间以使其持久,以便下次打开应用程序时,我之前创建的房间将可见。在我的notifyRoomCreationOk方法中,我使用了sendDefaultRoomConfig,它基本上描述了本段第一句话中所描述的内容。
-(void)sendDefaultRoomConfig:(NSString *)room
{
NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];
[x addAttributeWithName:@"type" stringValue:@"submit"];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"http://jabber.org/protocol/muc#owner"];
[query addChild:x];
XMPPIQ *iq = [XMPPIQ iq];
[iq addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"inroom-cr%@", room]];
[iq addAttributeWithName:@"to" stringValue:room];
[iq addAttributeWithName:@"type" stringValue:@"set"];
[iq addChild:query];
[[self xmppStream ] sendElement:iq];
}确保在调用上述方法的视图上启用了XMPPStream,否则这些方法将不起作用。非那样做不行。祝你XMPP玩得开心!
发布于 2012-04-30 14:32:44
XMPPRoom *room = [[XMPPRoom alloc] initWithRoomName:@"user101@conference.jabber.org/room" nickName:@"room"];
[room createOrJoinRoom];
[room sendInstantRoomConfig];
[room setInvitedUser:@"ABC@jabber.org"];
[room activate:[self xmppStream]];
[room inviteUser:jid1 withMessage:@"hello please join."];
[room sendMessage:@"HELLO"];用户ABC@jabber.org应收到邀请消息
发布于 2014-10-10 02:17:16
你的帖子是旧的,但现在我会这样做:
- (void)createRoomWithJid:(XMPPJID*)roomJID
{
XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self.xmppRoomHybridStorage
jid:roomJID
dispatchQueue:dispatch_get_main_queue()];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom activate:self.xmppStream];
[xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user
history:nil
password:nil];
}https://stackoverflow.com/questions/10371550
复制相似问题