Ios XMPPFramework-实现组聊天(MUC)
我正在使用iOS聊天客户端。有人能帮我进行多用户聊天吗 我已经实现了Robbiehanson的XMPPFramework 任何人都可以让我知道如何获得组列表,并创建一个组在服务器与此框架Ios XMPPFramework-实现组聊天(MUC),ios,objective-c,xmpp,xmppframework,Ios,Objective C,Xmpp,Xmppframework,我正在使用iOS聊天客户端。有人能帮我进行多用户聊天吗 我已经实现了Robbiehanson的XMPPFramework 任何人都可以让我知道如何获得组列表,并创建一个组在服务器与此框架 提前谢谢。获取房间列表: NSString* server = @"chat.shakespeare.lit"; //or whatever the server address for muc is XMPPJID *servrJID = [XMPPJID jidWithString:server]; XMP
提前谢谢。获取房间列表:
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];
检查委托方法中的响应:
加入或创建文件室
XMPPRoomMemoryStorage * _roomMemory = [[XMPPRoomMemoryStorage alloc]init];
NSString* roomID = @"roomExample@chat.shakespeare.lit";
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];
检查XMPPRoom委托方法中的响应:
更新
配置文件室:
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];
之后:
[xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user
history:history
password:nil];
加:
并检查响应:
- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{
DDLogVerbose(@"%@: %@ -> %@", THIS_FILE, THIS_METHOD, sender.roomJID.user);
}
查看configForm
对象,并根据需要进行更改,然后将其与[sender configureRoomUsingOptions:newConfig]一起发送代码>
示例:
要更改配置以使文件室持久化,可以添加以下内容:
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];
(我不熟悉NSXMLElement,所以可能有更好的方法来更改值)+(void)getGroupRooms{
n错误*错误=nil;
NSXMLElement*查询=[[NSXMLElement alloc]initWithXMLString:@“错误:&error];
XMPPIQ*iq=[XMPPIQ iqWithType:@“get”到:[XMPPJID JID WITHSTRING:Jabber_groupChat_Domain_server]元素ID:[[PXMPPManager sharedInstance]xmppStream]GenerateUID]子:查询];
[iq addAttributeWithName:@“from”stringValue:[[[PXMPPManager sharedInstance]xmppStream]myJID].full];
[[[PXMPPManager sharedInstance]xmppStream]sendElement:iq];
//
//
//
}
#pragma-标记XMPPStreamDelegate方法
-(BOOL)xmppStream:(xmppStream*)发送方未接收iq:(XMPPIQ*)iq{
//NSLog(@“确实收到IQ”);
如果([iq isResultIQ])
{
如果([iq elementForName:@“查询”xmlns:@)http://jabber.org/protocol/disco#items"])
{
NSLog(@“Jabber服务器的功能:%@,[iq XMLString]);
NSXMLElement*queryElement=[iq elementForName:@“查询”xmlns:@”http://jabber.org/protocol/disco#items"];
NSArray*items=[queryElement元素名称:@“item”];
NSMUTABLEARRY*arrGroupName=[[NSMUTABLEARRY alloc]init];
用于(项目中的NSXMLElement*i){
NSString*roomName=[i attributeStringValueForName:@“name”];
NSString*jidString=[i attributeStringValueForName:@“jid”];
//XMPPJID*jid=[XMPPJID jidWithString:jidString];
NSDictionary*dict=@{
@“组名”:roomName,
@“群组jid”:jidString,
};
[arrGroupName addObject:dict];
}
[ConversationsModel saveGroupName:arrGroupName];
}
}
返回false;
}
以下是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")
}
谢谢你的回答,它确实对我有用。你能告诉我如何设置不同类型的房间吗?我想创建一个持久的房间,就像whatsapp中的群组一样,可以邀请人聊天。如果您对此有任何想法,请告诉我。请查看[xmppRoom configureRoomUsingOptions:(nsxmlement*)roomConfigForm]
。要使用您想要的文件室配置创建nsxmlement
,请参考:还要查看示例159感谢Flores的评论,我确实查看了服务器文档并阅读了其中的内容,但无法了解如何配置它。我试图通过表单进行配置,但没有效果。如果你有代码或任何其他文件将是巨大的帮助…谢谢你的评论弗洛雷斯,它真的帮助了我很多。我有你的代码,它会给我所有聊天室的列表,但有没有办法只获得用户创建的聊天室或他是聊天室成员的聊天室。大家好,谢谢大家和starckoverflow,我可以创建组并通过两种存储(核心数据和内存存储)向其他人发送邀请。问题是,当我创建第二个组时,它会从核心数据存储中删除第一个组的数据,以及我们如何自动加入其他用户?大家好,谢谢大家和starckoverflow,我能够创建组并使用这两个存储(核心数据和内存存储)向其他用户发送邀请。问题是,当我创建第二个组时,它会从核心数据存储中删除第一个组的数据,我们如何自动加入其他用户?您能帮我解决这个@dichen吗?
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];
+(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="919414184320@cnr-uat.panamaxil.com/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;
}
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")
}