MUC如何与XMPPFramework

我正在开发一个利用Robbie Hanson的XMPPFramework的iOS XMPP聊天应用程序。

最重要的function已经实现 – 发送和接收消息。 基本上,我已经build立了一个基本的function聊天应用程序,当然有一点眼睛糖果。

现在,我遇到的问题是关于MUC。 我从其他网站看到的代码显示XMPPRoom中有一个initWithRoomName方法。 但是,这个方法在我克隆的git仓库中是不存在的。 那么,有什么替代呢? 或者,如果没有,我怎样才能使用XMPPFramework创build房间?

谢谢。

以下是我如何解决自己的问题。 请注意,此解决scheme根本不涉及XMPPRoom。 首先,我创build了一种方法,根据情况创build或进入一个房间。 (根据XMPP文档,创build的XML请求与您要进入房间的XML请求相同;也就是说,如果房间在input时尚不存在,则该服务将为您创build。)

开始了。 这是创build/进入房间的方法。 这个方法所做的就是发送一个到你想要创build/进入的房间。 如果您是第一个进入房间而房间尚未build立,则自动成为其拥有者和主持人。

 - (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方法中收到的XML响应。 如果状态码是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]; } } } 

然后,我们告诉服务器,我们正在创build一个“即时”types的房间,这意味着我们将发送一个智能元素,告诉它房间的默认设置。 notifyRoomCreationOk是在创build成功时在不同视图中调用的委托方法,毕竟我必须在文本文件中logging该房间以使其持久,以便下次打开之前创build的房间时,应用程序将可见。 在我的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-ing!

  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应该收到邀请信息

你的post是旧的,但现在我会这样做:

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

使用XMPPFRAMWORK通过以下代码创build聊天室。

  let roomStorage: XMPPRoomMemoryStorage = XMPPRoomMemoryStorage() /** * Remember to add 'conference' in your JID like this: * eg uniqueRoomJID@conference.yourserverdomain */ let roomJID: XMPPJID = XMPPJID.jidWithString("chatRoom_name@conference.myhostname") let xmppRoom: XMPPRoom = XMPPRoom(roomStorage: roomStorage, jid: roomJID, dispatchQueue: dispatch_get_main_queue()) xmppRoom.activate(SKxmpp.manager().xmppStream) xmppRoom.addDelegate(self, delegateQueue: dispatch_get_main_queue()) xmppRoom.joinRoomUsingNickname(SKxmpp.manager().xmppStream.myJID.user, history: nil, password: nil) xmppRoom.fetchConfigurationForm()