iOS XMPP群聊方法的实现

首先需要创建一个房间:

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基础中有,不做赘述。此为初步,后续补充。




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值