使用 xmpp 我可以创建群组并向 friend 发送邀请,但是当我在群组上发送消息时,成员将永远不会收到该消息。
成员(member)必须接受邀请吗?如果是,请告诉我怎么做?
请引用下面的代码,如果我犯了任何错误或仍然遗漏任何东西,请指导我,以便在我可以在群组中发送和接收消息并与 friend 聊天之后。
下面我附上了一些代码片段,用于在 xmpp 中创建组并发送消息。
[self setUpRoom:[NSString stringWithFormat"%@@conference.myserver",@"GroupName"]];
-(void)setUpRoomNSString *)ChatRoomJID {
// Configure xmppRoom
XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
XMPPJID *roomJID = [XMPPJID jidWithString:ChatRoomJID];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage jid:roomJID dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom joinRoomUsingNickname:_ro(@"LoginNumber")
history:nil
password:nil];
[self performSelectorselector(ConfigureNewRoom withObject:nil afterDelay:4];
}
现在房间确认我使用了这个片段
- (void)ConfigureNewRoomid)sender
{
[xmppRoom configureRoomUsingOptions:nil];
[xmppRoom fetchConfigurationForm];
[xmppRoom fetchBanList];
[xmppRoom fetchMembersList];
[xmppRoom fetchModeratorsList];
}
XMPP Room 委托(delegate)方法
- (void)xmppRoomDidCreateXMPPRoom *)sender
{
DDLogInfo(@"%@: %@", THIS_FILE, THIS_METHOD);
// I am inviting friends after room is created
for (int i = 0; i<[self.friendListArray count]; i++)
{
NSString * tempStr=[NSString stringWithFormat"%@@myserver",[[self.friendListArray objectAtIndex:i] valueForKey"UserNumber"]];
[sender inviteUser:[XMPPJID jidWithString:tempStr] withMessage"Greetings!"];
}
}
- (void)xmppRoomDidJoinXMPPRoom *)sender
{
DDLogInfo(@"%@: %@", THIS_FILE, THIS_METHOD);
NSLog(@"........Room Did join.......");
}
- (void)xmppRoomXMPPRoom *)sender didFetchConfigurationFormNSXMLElement *)configForm
{
DDLogInfo(@"%@: %@", THIS_FILE, THIS_METHOD);
NSXMLElement *newConfig = [configForm copy];
NSArray *fields = [newConfig elementsForName"field"];
for (NSXMLElement *field in fields)
{
NSString *var = [field attributeStringValueForName"var"];
// Make Room Persistent
if ([var isEqualToString"muc#roomconfig_persistentroom"])
{
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName"value" stringValue"1"]];
}
if ([var isEqualToString:@"roomconfig_enablelogging"])
{
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
}
if ([var isEqualToString:@"muc#roomconfig_maxusers"])
{
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"100"]];
}
}
[sender configureRoomUsingOptions:newConfig];
}
在按钮点击时分组发送消息以进行测试
-(void)sendGroupMessage
{
[xmppRoom sendMessageWithBody:@"Hi All"];
NSXMLElement *x = [NSXMLElement elementWithName:@"groupchat" xmlns:XMPPMUCNamespace];
XMPPMessage *message = [XMPPMessage message];
[message addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"%@/%@",[xmppRoom.roomJID full],_ro(@"LoginNumber")]];
[message addChild:x];
NSLog(@"x in Invite === %@",x);
[xmppStream sendElement:message];
}
代替:
[xmppStream sendElement:message]
尝试:
[xmppRoom sendMessage:message]
关于ios - 无法在 iphone 中使用 Xmpp 在群聊中发送和接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24993186/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |