XMPP通信中的离线消息

ConnectionConfiguration config = new ConnectionConfiguration("192.168.1.100", 5222);
		config.setSecurityMode(SecurityMode.disabled);
		config.setDebuggerEnabled(true);
		config.setSendPresence(false);
		XMPPConnection con = new XMPPTCPConnection(config);
		con.setPacketReplyTimeout(5000);
		con.connect();
		con.login("admin", "0", "潘迪安");
		Chat chat = ChatManager.getInstanceFor(con).createChat("yy@192.168.1.100", new MessageListener() {
			@Override
			public void processMessage(Chat chat, Message message) {
				System.out.println("Received message: " + message);
			}
		});
		//离线文件
		OfflineMessageManager offMM = new OfflineMessageManager(con);
		System.out.println("离线文件数量 :" + offMM.getMessageCount());
		System.out.println("离线文件内容 :");
//经测试,当调用getMessages时,会触发chat设置的监听器,从而输出离线消息内容,但是getMessages方法返回的离线消息为空
		List<Message> listMessage = offMM.getMessages();
		System.out.println(listMessage.size());
		for(Message m : offMM.getMessages()) {
			System.out.println(" 离线  : " + m.getBody() + m.getBodies());
		}
进入getMessage方法中
<pre name="code" class="java"> public List<Message> getMessages() throws NoResponseException, XMPPErrorException, NotConnectedException {
        List<Message> messages = new ArrayList<Message>();
        OfflineMessageRequest request = new OfflineMessageRequest();
        request.setFetch(true);
        connection.createPacketCollectorAndSend(request).nextResultOrThrow();
//根据Filter确定要把什么形式的消息放在collector中
        PacketCollector messageCollector = connection.createPacketCollector(packetFilter);
        // Collect the received offline messages
//debug下看到message为空
        Message message = (Message) messageCollector.nextResult();
        while (message != null) {
            messages.add(message);
            message = (Message) messageCollector.nextResult();
        }
        // Stop queuing offline messages
        messageCollector.cancel();
        return messages;
    }


 
 public PacketCollector createPacketCollector(PacketFilter packetFilter) {
        PacketCollector collector = new PacketCollector(this, packetFilter);
        // Add the collector to the list of active collectors.
        collectors.add(collector);
        return collector;
    }

  protected PacketCollector(XMPPConnection connection, PacketFilter packetFilter) {
        this(connection, packetFilter, SmackConfiguration.getPacketCollectorSize());
    }

 protected PacketCollector(XMPPConnection connection, PacketFilter packetFilter, int maxSize) {
        this.connection = connection;
        this.packetFilter = packetFilter;
        this.resultQueue = new ArrayBlockingQueue<Packet>(maxSize);
    }



所以得到的结果是离线消息的数目是实际消息数目,但是通过getMessages得到的list的size为0

离线的消息在chat中的listener中可以看见

每一次执行getMessages,在listener中可以输出一次消息

但是在上述的几个方法中都没有看见回调listener的方法,由此想到回调函数的另一种方式

用while(true)监听一个变量,如果改变则执行回调

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值