xmppframework 群聊功能简单实现

本文主要说的是ios客户端。服务端的默认已经构建好。

//主要的关键是:
//通过
//-(void)xmppRoom:(XMPPRoom *)sender didReceiveMessage:(XMPPMessage *)message fromOccupant:(XMPPJID *)occupantJID
//接收到消息
//
//在该方法中调用代理方法
//-(void)newMessageReceived:(NSDictionary *)message


1、下载xmppframework(googol一下就出来了)

2、导入到你的工程(参照github)

3、在AppDelegate中实现xmpp代理

  (1)、初始化XMPPStream

-(void)setupStream{
    //初始化XMPPStream
    xmppStream = [[XMPPStream alloc] init];
    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    xmppPing = [[XMPPPing alloc]init];
    xmppPing.respondsToQueries = YES;
    [xmppPing activate:xmppStream];
    xmppReconnect = [[XMPPReconnect alloc]init];
    xmppReconnect.autoReconnect = YES;
    [xmppReconnect activate:xmppStream];
}

(3)、登录xmpp消息服务器

//登录XMPP服务器
-(BOOL)login:(NSString*)userid code:(NSString*)passcode{
    [self setupStream];
    if (![xmppStream isDisconnected]) {
        [self.chatDelegate didConnect];
        return YES;
    }
    NSString *server = ChatHost;
    //设置用户
    NSString *userId = [NSString stringWithFormat:@"%@@%@",userid,server];
    XMPPJID *jid = [XMPPJID jidWithString:userId];
    [xmppStream setMyJID:jid];
    //设置服务器
    [xmppStream setHostName:server];
    //密码
    password = passcode;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:userid forKey:USER];
    [defaults setObject:passcode forKey:PSW];
    
    //连接服务器
    NSError *error = nil;
    
    if(![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]){
        NSLog(@"cant connect %@", server);
        return NO;
    }

    return YES;
}


  (2)、初始化聊天室

-(void)initxmpproom{
    [self getrooms];
    xmppRoomStorage  = [XMPPRoomCoreDataStorage sharedInstance];
    XMPPJID *roomJID = [XMPPJID jidWithString:CHATROOM];
    xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:xmppRoomStorage jid:roomJID];
    [xmppRoom activate:xmppStream];
    [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
    //加入房间
    [self joinroom];
}

(3)、加入房间

-(void)joinroom{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [xmppRoom joinRoomUsingNickname:[defaults stringForKey:USER] history:nil];
}


(4)、 有人在群里发言(实现代理方法)

-(void)xmppRoom:(XMPPRoom *)sender didReceiveMessage:(XMPPMessage *)message fromOccupant:(XMPPJID *)occupantJID{
    NSLog(@"群发言了。。。。");
    
    NSString *type = [[message attributeForName:@"type"] stringValue];
    if ([type isEqualToString:@"groupchat"]) {
        NSString *msg = [[message elementForName:@"body"] stringValue];
        NSString *timexx = [[timex attributeForName:@"stamp"] stringValue];
        NSString *from = [[message attributeForName:@"from"] stringValue];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:msg forKey:@"body"];
        [dict setObject:from forKey:@"from"];
     
        //消息委托
        [messageDelegate newMessageReceived:dict];
    }
    
}

(5)、 新人加入群聊

- (void)xmppRoom:(XMPPRoom *)sender occupantDidJoin:(XMPPJID *)occupantJID
{
    NSLog(@"新人加入群聊");
}

(6)、 有人退出群聊

- (void)xmppRoom:(XMPPRoom *)sender occupantDidLeave:(XMPPJID *)occupantJID
{
    NSLog(@"有人退出群聊");
}


(7)、发送新的群聊消息

//发送群消息
- (IBAction)sendPress:(UIButton *)sender {
    //本地输入框中的信息
    NSString *message = self.sendtextfield.text;
    self.sendtextfield.text = @"";
    [self.sendtextfield resignFirstResponder];
    
    if (message.length > 0){
        NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
        [body setStringValue:message];
        
        //生成XML消息文档
        NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
        
        //消息类型
        [mes addAttributeWithName:@"type" stringValue:@"groupchat"];
        
        //发送给谁
        [mes addAttributeWithName:@"to" stringValue:CHATROOM];
        
        //由谁发送
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        
        [mes addAttributeWithName:@"from" stringValue:[NSString stringWithFormat:@"%@/%@",CHATROOM,[defaults stringForKey:USER]]];
        
        //组合
        [mes addChild:body];
        
        //发送消息
        [[app xmppStream] sendElement:mes];
        
    }
}

(8)、

-(void)newMessageReceived:(NSDictionary *)messageContent{
    [arr1 addObject:messageContent];
    [self.tableview1 reloadData];
    
    if (self.tableview1.contentSize.height > self.tableview1.frame.size.height) {
        [self.tableview1 setContentOffset:CGPointMake(0, self.tableview1.contentSize.height - self.tableview1.frame.size.height)];
    }
}





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值