ios使用XMPPFramework 来实现基本的聊天功能
1.使用xmppStream连接openfire
需要账户,密码和服务器地址端口一般为5222
-(BOOL)connect{
[self setupStream];//设置xmppStream
//从本地取得用户名,密码和服务器地址
NSString *userId =USERID;
NSString *pass =PASSWORD;//使用登陆时访问服务器获得的密码
NSString *server =SERVER;
if (![xmppStream isDisconnected]) {
return YES;
}
if (userId == nil || pass == nil) {
return NO;
}
//设置用户
[xmppStream setMyJID:[XMPPJID jidWithString:userId]];
//设置服务器
[xmppStream setHostName:server];
[xmppStream setHostPort:5222];
//密码
password = pass;
//连接服务器
NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {
NSLog(@"cant connect %@", server);
return NO;
}
return YES;
}
-(void)setupStream{
self.xmppStream = [[XMPPStream alloc] init];
//在主线程中运行
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
2,发送消息,通过XML来发送
XMPPFramework主要是通过KissXML来生成XML文件
//生成<body>文档
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
//生成XML消息文档
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
//消息类型
[mes addAttributeWithName:@"type" stringValue:@"chat"];
//发送给谁
[mes addAttributeWithName:@"to" stringValue:[NSString
stringWithFormat:@"%@@localhost",chatWithUserId]];
[mes addAttributeWithName:@"from" stringValue:[NSString stringWithFormat:@"%@@localhost",MYYUEYUEID]];
//组合 将body作为message的子节点
[mes addChild:body];
//发送消息
[[self xmppStream] sendElement:mes];
3.接受消息
在xmppStream连接过程中有代理方法,可以在收到消息后发回
//收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
NSString *type = [[message attributeForName:@"type"] stringValue];
if ([type isEqualToString:@"error"])
{
return;
}
NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:msg forKey:@"msg"];
[dict setObject:from forKey:@"sender"];
//消息接收到的时间
[dict setObject:[Statics getCurrentTime] forKey:@"time"];
·····
//··dict 加入数据库··或显示在界面·· 根据状态决定
·····
}
1.使用xmppStream连接openfire
需要账户,密码和服务器地址端口一般为5222
-(BOOL)connect{
[self setupStream];//设置xmppStream
//从本地取得用户名,密码和服务器地址
NSString *userId =USERID;
NSString *pass =PASSWORD;//使用登陆时访问服务器获得的密码
NSString *server =SERVER;
if (![xmppStream isDisconnected]) {
return YES;
}
if (userId == nil || pass == nil) {
return NO;
}
//设置用户
[xmppStream setMyJID:[XMPPJID jidWithString:userId]];
//设置服务器
[xmppStream setHostName:server];
[xmppStream setHostPort:5222];
//密码
password = pass;
//连接服务器
NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {
NSLog(@"cant connect %@", server);
return NO;
}
return YES;
}
-(void)setupStream{
self.xmppStream = [[XMPPStream alloc] init];
//在主线程中运行
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
2,发送消息,通过XML来发送
XMPPFramework主要是通过KissXML来生成XML文件
//生成<body>文档
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
//生成XML消息文档
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
//消息类型
[mes addAttributeWithName:@"type" stringValue:@"chat"];
//发送给谁
[mes addAttributeWithName:@"to" stringValue:[NSString
stringWithFormat:@"%@@localhost",chatWithUserId]];
[mes addAttributeWithName:@"from" stringValue:[NSString stringWithFormat:@"%@@localhost",MYYUEYUEID]];
//组合 将body作为message的子节点
[mes addChild:body];
//发送消息
[[self xmppStream] sendElement:mes];
3.接受消息
在xmppStream连接过程中有代理方法,可以在收到消息后发回
//收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
NSString *type = [[message attributeForName:@"type"] stringValue];
if ([type isEqualToString:@"error"])
{
return;
}
NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:msg forKey:@"msg"];
[dict setObject:from forKey:@"sender"];
//消息接收到的时间
[dict setObject:[Statics getCurrentTime] forKey:@"time"];
·····
//··dict 加入数据库··或显示在界面·· 根据状态决定
·····
}