Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

ios - Accepting chatroom invitation

I'm able to create a MUC using XMPPFramework and send user invitation requests to join that room by using the code below.

// Creating
AppDelegate *dele =(AppDelegate *) [[UIApplication sharedApplication]delegate];

xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:dele jid:[XMPPJID jidWithString:self.roomName] dispatchQueue:dispatch_get_main_queue()];
[xmppRoom addDelegate:dele delegateQueue:dispatch_get_main_queue()];
[xmppRoom activate:dele.xmppStream];
[xmppRoom joinRoomUsingNickname:self.myNick history:nil];

// Inviting
[xmppRoom inviteUser:[XMPPJID jidWithString:@"abc@host"] withMessage:@"Come Join me"];

How does a user "abc" know that he has received an invitation and how can he react to it either by accepting or declining?

I could not find any class in XMPPFramework which directly deal with chat room invitation. My research says that whenever a user receives an chatroom invitation, xmmppStream's delegate method is called:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message

In that message, I check whether the it contains NSXMLElement with name "invite", and if it contains then i send a callback to the user. Then i create chatroom with the same name as the name of the chatroom from which user received invitation and enter that newly created room. It works fine but quiet lengthy and not quiet efficient. I want to know if there is a class in XMPPFramework available here which could handle chat room invitation separately. For instance, detecting, accepting, and declining of room invitations.

My code for extracting room name:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
    NSXMLElement * x = [message elementForName:@"x" xmlns:XMPPMUCUserNamespace];
    NSXMLElement * invite  = [x elementForName:@"invite"];
    NSXMLElement * decline = [x elementForName:@"decline"];
    NSXMLElement * directInvite = [message elementForName:@"x" xmlns:@"jabber:x:conference"];
    NSString *msg = [[message elementForName:@"body"]stringValue];
    NSString *from = [[[message attributeForName:@"from"]stringValue];
    if (invite || directInvite)
    {
        [self createAndEnterRoom:from Message:msg];
        return;
    }
    [self.delegate newMessageRecieved:msg];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For room invitations and declines, implement XMPPMUCDelegate and its methods -xmppMUC:didReceiveRoomInvitation: and -xmppMUC:didReceiveRoomInvitationDecline:.

To get the room JID, invoke [message from];

To join the room, instantiate an XMPPRoom and invoke -joinRoomUsingNickname:history:.

Then have your room delegate class implement XMPPRoomDelegate, and implement some of the delegate methods to handle receiving messages in the room.

It looks like there isn't at present a more automatic way to respond to invitations.

Update: The delegate callbacks now receive the room JID as a parameter, clarifying the semantics a bit.

- (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitation:(XMPPMessage *)message;
- (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitationDecline:(XMPPMessage *)message;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...