我在添加朋友、发送邀请以及在群聊中遇到困难,比如使用 xmpp 为所有人发送一条消息。我知道我需要使用 XEP-0045。但我没有成功。谁能告诉我该怎么做。
- 发送一对一聊天的好友请求。
- 发送邀请加入聊天室。
- 向聊天室的朋友发送消息。
如果有人有示例代码会很棒..
提前致谢
对于#3:向聊天室的朋友发送消息。
-(void) sendGroupMessage:(NSString *) groupJID Message:(NSString *)msg{
XMPPJID *roomJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@conference.%@",groupJID,SERVER_URL]];
XMPPRoom *muc = [[XMPPRoom alloc] initWithRoomStorage:xmppRoomStorage jid:roomJID
dispatchQueue:dispatch_get_main_queue()];
[muc activate:xmppStream];
[muc addDelegate:self delegateQueue:dispatch_get_main_queue()];
[muc sendMessageWithBody:msg];
}
第 2 点:发送邀请加入聊天室。
当您在以下委托 (xmppRoomDidCreate) 中收到响应时,您可以向其他人发送邀请:
- (void)xmppRoomDidCreate:(XMPPRoom *)xmppRoom
{
NSLog(@"xmppRoomDidCreate");
[xmppRoom inviteUser:[User JID Here] withMessage:@"Your Message Here"];
// You can send invitations in loop if you have multiple users to invite
}
Point 3:向聊天室的朋友发送消息。实际上,该消息是在组中广播的。一定会交付给小组中的所有成员。
- (void) sendMessageInGroup:(NSString *) message withGroupName:(NSString *) groupName
{
NSString * qualifiedGroupName = [NSString stringWithFormat:@"%@@%@", [groupName lowercaseString], SERVER_NAME];
// self.xmppRoomDetails consists of your muc rooms' objects i.i XMPPRoom
for (int i = 0; i < self.xmppRoomDetails.count; i++)
{
XMPPRoom * room = [self.xmppRoomDetails objectAtIndex:i];
XMPPJID * myRoomJID = room.myRoomJID;
NSString * roomName = [NSString stringWithFormat:@"%@@%@", [myRoomJID.user lowercaseString], SERVER_NAME];
if ([qualifiedGroupName rangeOfString:roomName].location != NSNotFound)
{
XMPPRoom * sendMessageWithRoom = [self.xmppRoomDetails objectAtIndex:i];
[sendMessageWithRoom sendMessage:message];
}
}
}