ios objective-c xmpp xmppframework

ios - XMPPFramework-Implementar chat en grupo(MUC)



objective-c (3)

Aquí hay una versión Swift:

func joinRoom(with jidString: String, delegate: XMPPRoomDelegate) -> XMPPRoom { let roomJID = XMPPJID(string: jidString) let roomStorage = XMPPRoomCoreDataStorage.sharedInstance() let room = XMPPRoom(roomStorage: roomStorage, jid: roomJID, dispatchQueue: DispatchQueue.main)! room.activate(xmppStream) room.addDelegate(delegate, delegateQueue: DispatchQueue.main) // If the room is not existing, server will create one. room.join(usingNickname: xmppStream.myJID.user, history: nil) return room } // MUCRoomDelegate public func xmppRoomDidCreate(_ sender: XMPPRoom!) { print("xmppRoomDidCreate") // I prefer configure right after created sender.fetchConfigurationForm() } public func xmppRoomDidJoin(_ sender: XMPPRoom!) { print("xmppRoomDidJoin") } public func xmppRoom(_ sender: XMPPRoom!, didFetchConfigurationForm configForm: DDXMLElement!) { print("didFetchConfigurationForm") let newForm = configForm.copy() as! DDXMLElement for field in newForm.elements(forName: "field") { if let _var = field.attributeStringValue(forName: "var") { switch _var { case "muc#roomconfig_persistentroom": field.remove(forName: "value") field.addChild(DDXMLElement(name: "value", numberValue: 1)) case "muc#roomconfig_membersonly": field.remove(forName: "value") field.addChild(DDXMLElement(name: "value", numberValue: 1)) // other configures default: break } } } sender.configureRoom(usingOptions: newForm) } public func xmppRoom(_ sender: XMPPRoom!, didConfigure iqResult: XMPPIQ!) { print("didConfigure") }

Estoy trabajando en el cliente de chat iOS. ¿Alguien puede ayudarme con el chat multiusuario?

He implementado XMPPFramework de Robbiehanson.

¿Alguien puede, por favor, decirme cómo obtener una lista de grupos y crear un grupo en el servidor con este marco?

Gracias por adelantado.


Para obtener una lista de habitaciones:

NSString* server = @"chat.shakespeare.lit"; //or whatever the server address for muc is XMPPJID *servrJID = [XMPPJID jidWithString:server]; XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:servJID]; [iq addAttributeWithName:@"from" stringValue:[xmppStream myJID].full]; NSXMLElement *query = [NSXMLElement elementWithName:@"query"]; [query addAttributeWithName:@"xmlns" stringValue:@"http://jabber.org/protocol/disco#items"]; [iq addChild:query]; [xmppStream sendElement:iq];

Compruebe la respuesta en el método de delegado:

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{ DDLogVerbose(@"%@", [iq description]); return NO; }

unirse o crear espacio

XMPPRoomMemoryStorage * _roomMemory = [[XMPPRoomMemoryStorage alloc]init]; NSString* roomID = @"[email protected]"; XMPPJID * roomJID = [XMPPJID jidWithString:roomID]; XMPPRoom* xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:_roomMemory jid:roomJID dispatchQueue:dispatch_get_main_queue()]; [xmppRoom activate:self.xmppStream]; [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()]; [xmppRoom joinRoomUsingNickname:@"myNickname" history:nil password:nil];

Verifique la respuesta en los métodos de delegado de XMPPRoom:

- (void)xmppRoomDidCreate:(XMPPRoom *)sender{ DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); } - (void)xmppRoomDidJoin:(XMPPRoom *)sender{ DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); }

actualizar

para configurar una habitación:

después:

[xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user history:history password:nil];

añadir:

[xmppRoom fetchConfigurationForm];

y verifique la respuesta en:

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{ DDLogVerbose(@"%@: %@ -> %@", THIS_FILE, THIS_METHOD, sender.roomJID.user); }

Revise el objeto configForm y cambie según sea necesario, luego envíelo con [sender configureRoomUsingOptions:newConfig];

Ejemplo: para cambiar la configuración y hacer que la habitación sea persistente, puede agregar algo como:

NSXMLElement *newConfig = [configForm copy]; NSArray* fields = [newConfig elementsForName:@"field"]; for (NSXMLElement *field in fields) { NSString *var = [field attributeStringValueForName:@"var"]; if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) { [field removeChildAtIndex:0]; [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]]; } } [sender configureRoomUsingOptions:newConfig];

(No estoy familiarizado con NSXMLElement, así que quizás haya una mejor manera de cambiar el valor)


+(void)getGroupRooms{ NSError *error = nil; NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns=''http://jabber.org/protocol/disco#items''/>" error:&error]; XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:[XMPPJID jidWithString:Jabber_groupChat_Domain_server] elementID:[[[PXMPPManager sharedInstance] xmppStream] generateUUID] child:query]; [iq addAttributeWithName:@"from" stringValue:[[[PXMPPManager sharedInstance] xmppStream] myJID].full]; [[[PXMPPManager sharedInstance] xmppStream] sendElement:iq]; //<iq type="get" //to="conference.cnr-uat.panamaxil.com" //id="DF27F28E-488D-4DAB-AA03-399A4CDE91B3" //from="[email protected]/iphone"> //<query xmlns="http://jabber.org/protocol/disco#items"/> //</iq> } #pragma - mark XMPPStreamDelegate Methods - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{ // NSLog(@"Did receive IQ"); if([iq isResultIQ]) { if([iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"]) { NSLog(@"Jabber Server''s Capabilities: %@", [iq XMLString]); NSXMLElement *queryElement = [iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"]; NSArray *items = [queryElement elementsForName:@"item"]; NSMutableArray *arrGroupName = [[NSMutableArray alloc] init]; for (NSXMLElement *i in items) { NSString *roomName = [i attributeStringValueForName:@"name"]; NSString *jidString = [i attributeStringValueForName:@"jid"]; //XMPPJID *jid = [XMPPJID jidWithString:jidString]; NSDictionary *dict = @{ @"groupName" : roomName, @"groupJID" : jidString, }; [arrGroupName addObject:dict]; } [ConversationsModel saveGroupName:arrGroupName]; } } return false; }