原始地址:XMPPFrameWork IOS 开发(四)

消息

//收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{ // NSLog(@"message = %@", message);
//消息的内容
NSString *msg = [[message elementForName:@"body"] stringValue];
//消息发送者
NSString *from = [[message attributeForName:@"from"] stringValue]; /****在此处****/
//通知聊天页面有新消息,需要处理 }

发送消息

//发送消息的xml格式
<message from='发送者账号'
to='接收者账号'
type='chat'>
<body>HELLO WORLD </body>
</message>

//代码组装

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];

好友上下线通知

- (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"])//用户不在线
{
//列表和数据库都要相应改变
}
}
}

最新文章

  1. C# mvc DropDownList选中状态无效情况分析
  2. ---解决git pull 后出现冲突的解决方法
  3. sql 循环语句几种方式
  4. 洛谷P1457 城堡 The Castle
  5. 软件project(六)——需求分析
  6. 不可不知的 Android strings.xml 那些事
  7. sockt初级了解 感悟 一起打怪升级偶
  8. xilinx和altera复位电平
  9. react 脚手架 立即可以写业务 react + react-router-dom + less + axios + antd
  10. 在.NET Core console application中使用User Secrets(用户机密)
  11. C++与Java,C#的异同(一):值,地址,引用
  12. 第3章 Git使用人门
  13. Java并发(一)并发编程的挑战
  14. 11i and R12 Table Count in Different Module
  15. Winsock解析
  16. SQL触发器操作
  17. Windows上Tomcat启动,服务中没有Tomcat
  18. 处理Model
  19. 设计模式--Restful笔记(一)
  20. 【剑指offer】05替换空格,C++实现

热门文章

  1. HDU 6186 CS Course
  2. Java常用工具类之Excel导出
  3. 深入理解javascript作用域系列第三篇
  4. misaka and last order SCU - 4489 (筛法的灵活应用)
  5. hdu 1180 诡异的楼梯(优先队列)
  6. loj2576 「TJOI2018」str
  7. hdu 5234 Happy birthday 背包 dp
  8. Codeforces Round #302 (Div. 2) B. Sea and Islands 构造
  9. UIImagePickerController导航字体颜色和背景
  10. mybatis源码分析(6)-----核心调度对象StatmentHandler