本文主要说的是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];
}
-(void)joinroom{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[xmppRoom joinRoomUsingNickname:[defaults stringForKey:USER] history:nil];
}
-(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];
}
}
- (void)xmppRoom:(XMPPRoom *)sender occupantDidJoin:(XMPPJID *)occupantJID
{
NSLog(@"新人加入群聊");
}
- (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)];
}
}