• XMPP系列(四)---发送和接收文字消息,获取历史消息功能


    今天開始做到最基本的功能发送和接收消息、获取本地历史数据。

    先上到眼下为止的效果图:

                

    首先是要在XMPPFramework.h中引入数据存储模块:

    //聊天记录模块的导入
    #import "XMPPMessageArchiving.h"
    #import "XMPPMessageArchivingCoreDataStorage.h"
    #import "XMPPMessageArchiving_Contact_CoreDataObject.h" //近期联系人
    #import "XMPPMessageArchiving_Message_CoreDataObject.h"

    然后在XMPPStream中加入数据存储模块。由于XMPP框架默认用CoreData存储数据,并且做好了数据存储,因此我们仅仅须要在合适的地方发送通知处理信息就可以。

     //4.消息模块。这里用单例,不能切换账号登录,否则会出现数据问题。

    _xmppMessageArchivingCoreDataStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance]; _xmppMessageArchiving = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:_xmppMessageArchivingCoreDataStorage dispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 9)]; [_xmppMessageArchiving activate:self.xmppStream];


    在数据存储完成后,发送通知:

    XMPPMessageArchivingCoreDataStorage的

    - (void)archiveMessage:(XMPPMessage *)message outgoing:(BOOL)isOutgoing xmppStream:(XMPPStream *)xmppStream结尾处加入例如以下通知:

        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:kXMPP_MESSAGE_CHANGE object:nil];
        });
    如今去聊天界面获取历史消息吧:

    /** 查询聊天记录 */
    - (void)getChatHistory
    {
        XMPPMessageArchivingCoreDataStorage *storage = [JKXMPPTool sharedInstance].xmppMessageArchivingCoreDataStorage;
        //查询的时候要给上下文
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:storage.messageEntityName inManagedObjectContext:storage.mainThreadManagedObjectContext];
        [fetchRequest setEntity:entity];
        // Specify criteria for filtering which objects to fetch
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bareJidStr = %@", self.chatJID.bare];
        [fetchRequest setPredicate:predicate];
        // Specify how the fetched objects should be sorted
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp"
                                                                       ascending:YES];
        [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
        
        NSError *error = nil;
        NSArray *fetchedObjects = [storage.mainThreadManagedObjectContext executeFetchRequest:fetchRequest error:&error];
        if (fetchedObjects != nil) {
            self.chatHistory = [[NSMutableArray alloc] initWithArray:fetchedObjects];
            //        [NSMutableArray arrayWithArray:fetchedObjects];
        }
        
        [self.messageTableView reloadData];
        
        [self tableViewScrollToBottom];
    }
    

    消息接收也调用getChatHistory方法就可以

    而消息的发送是:

    /** 发送的事件 */
    - (void)sendMessage{
        if (_chatTextField.text.length < 1) {
            return;
        }
        XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:self.chatJID];
        [message addBody:_chatTextField.text];
        [[JKXMPPTool sharedInstance].xmppStream sendElement:message];
        
        _chatTextField.text = @"";
        
        [self tableViewScrollToBottom];
    }

    demo地址:https://github.com/Joker-King/ChatDemo

  • 相关阅读:
    python运算学习之Numpy ------ 算术运算
    一招教你如何在简历上突出工作经验!
    【软件测试工程师】跳槽到大厂需要具备什么资历和技能?
    LoadRunner性能测试系统学习教程:脚本编写之参数化技术(4)
    Python+Selenium自动化测试教程连载(3)
    测试人员在职场中如何提升自己的沟通能力?
    LoadRunner性能测试系统学习教程:脚本编写之Block(块)技术与参数化(3)
    转行月入10K+的人,背后是付出怎么样的努力呢?
    LoadRunner性能测试系统学习教程:脚本编写之检查点函数(2)
    Python+Selenium自动化测试教程连载(2)
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7045304.html
Copyright © 2020-2023  润新知