iOS XMPP群聊方法的实现

来源:互联网 发布:淘宝开店半年怎么算 编辑:程序博客网 时间:2024/06/13 12:20

首先需要创建一个房间:

xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self jid:[XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@", jidString, @"conference.192.168.1.117"]]];    [xmppRoom activate:xmppStream];    [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];

加入房间:

[[self xmppRoom] joinRoomUsingNickname:[[[NSUserDefaults standardUserDefaults] objectForKey:@"UserInfo"] valueForKey:@"userId"] history:nil];

此时房间ID就出现在好友列表内,可以正常进入聊天。

其次邀请好友:

[[self xmppRoom] inviteUser:[XMPPJID jidWithString:[NSString stringWithFormat:@"%@@192.168.1.117", friendIDFeild.text]] withMessage:@"join room!"];

客户端要对收到邀请做出响应,方法如下:

新建一个XMPPMUC:

xmppMUC = [[XMPPMUC alloc] initWithDispatchQueue:dispatch_get_main_queue()];    [xmppMUC activate:xmppStream];    [xmppMUC addDelegate:self delegateQueue:dispatch_get_main_queue()];
类要实现XMPPMUCDelegate

-(void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *)roomJID didReceiveInvitation:(XMPPMessage *)message{    NSLog(@"%@", message);    xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self jid:roomJID];    [xmppRoom activate:xmppStream];    [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];    [xmppRoom joinRoomUsingNickname:[[[NSUserDefaults standardUserDefaults] objectForKey:@"UserInfo"] valueForKey:@"userId"] history:nil];}
其中
[xmppRoom joinRoomUsingNickname:[[[NSUserDefaults standardUserDefaults] objectForKey:@"UserInfo"] valueForKey:@"userId"] history:nil];

为确认加入房间。

当你接受请求后,房间id会出现在联系人列表中,正常聊天就可以了,聊天信息的type为groupchat,这部分在XMPP基础中有,不做赘述。此为初步,后续补充。




0 0
原创粉丝点击