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)监听一个变量,如果改变则执行回调