XMPPFrameWork IOS 开发(四)消息和好友上下线
2016-06-08 11:57
525 查看
消息
[cpp] view
plain copy
print?
//收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
// NSLog(@"message = %@", message);
//消息的内容
NSString *msg = [[message elementForName:@"body"] stringValue];
//消息发送者
NSString *from = [[message attributeForName:@"from"] stringValue];
/****在此处****/
//通知聊天页面有新消息,需要处理
}
发送消息
[cpp] view
plain copy
print?
//发送消息的xml格式
<message from='发送者账号'
to='接收者账号'
type='chat'>
<body>HELLO WORLD </body>
</message>
//代码组装
[cpp] view
plain copy
print?
NSString *message = @"HELLO WORLD";
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
//生成XML消息文档
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
//消息类型
[mes addAttributeWithName:@"type" stringValue:@"chat"];
//发送给谁
[mes addAttributeWithName:@"to" stringValue:@"接受者账号"];
//由谁发送
[mes addAttributeWithName:@"from" stringValue:@"发送者账号"];
//组合
[mes addChild:body];
//发送消息
[[self xmppStream] sendElement:mes];
好友上下线通知
[cpp] view
plain copy
print?
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
//取得好友状态
NSString *presenceType = [presence type]; //online/offline
//当前用户
NSString *userId = [[sender myJID] user];
//在线用户
NSString *presenceFromUser = [[presence from] user];
/*
//如果不是自己,如果涉及多段登录,此处最好加上else,如果是自己离线的话,调用上线协议
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
*/
if (![presenceFromUser isEqualToString:userId])
{
//用户在线
if ([presenceType isEqualToString:@"available"])
{
//列表和数据库都要相应改变
}else if ([presenceType isEqualToString:@"unavailable"])//用户不在线
{
//列表和数据库都要相应改变
}
[cpp] view
plain copy
print?
//收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
// NSLog(@"message = %@", message);
//消息的内容
NSString *msg = [[message elementForName:@"body"] stringValue];
//消息发送者
NSString *from = [[message attributeForName:@"from"] stringValue];
/****在此处****/
//通知聊天页面有新消息,需要处理
}
发送消息
[cpp] view
plain copy
print?
//发送消息的xml格式
<message from='发送者账号'
to='接收者账号'
type='chat'>
<body>HELLO WORLD </body>
</message>
//代码组装
[cpp] view
plain copy
print?
NSString *message = @"HELLO WORLD";
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
//生成XML消息文档
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
//消息类型
[mes addAttributeWithName:@"type" stringValue:@"chat"];
//发送给谁
[mes addAttributeWithName:@"to" stringValue:@"接受者账号"];
//由谁发送
[mes addAttributeWithName:@"from" stringValue:@"发送者账号"];
//组合
[mes addChild:body];
//发送消息
[[self xmppStream] sendElement:mes];
好友上下线通知
[cpp] view
plain copy
print?
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
//取得好友状态
NSString *presenceType = [presence type]; //online/offline
//当前用户
NSString *userId = [[sender myJID] user];
//在线用户
NSString *presenceFromUser = [[presence from] user];
/*
//如果不是自己,如果涉及多段登录,此处最好加上else,如果是自己离线的话,调用上线协议
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
*/
if (![presenceFromUser isEqualToString:userId])
{
//用户在线
if ([presenceType isEqualToString:@"available"])
{
//列表和数据库都要相应改变
}else if ([presenceType isEqualToString:@"unavailable"])//用户不在线
{
//列表和数据库都要相应改变
}
相关文章推荐
- [转][XMPP] gtalk & XMPP & libjingle
- IM 协议的分析和选取 (XMPP&WebSocket)
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- XMPP学习笔记(1)
- Deploying Control Plane Policing
- Ubuntu12.04(64bit)上部署编译运行Openfire+Spark环境
- 在Openfire源码中添加自己的插件
- Thrift的required和optional源码分析
- 关于sparkweb
- c++中extern关键字详解 知识补充笔记
- tigase的message流转
- MBProgressHUD的基本使用
- 意外的结果-C++中的移位操作
- wikipedia上的AMQP介绍
- 解决<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized></not-authorized></failure>
- iOS开发之使用XMPPFramework实现即时通信(一)
- iOS开发之使用XMPPFramework实现即时通信(二)
- iOS开发之使用XMPPFramework实现即时通信(三)