使用smack读取pubsub中的传入消息包

时间:2012-12-08 11:57:37

标签: android publish-subscribe openfire smack asmack

我正在尝试使用openfire服务器和asmack库实现pubsub。 我已经配置了我的节点,subscribers必须获得publishers的批准

现在我正在尝试使用以下代码读取Message数据包,但我没有收到任何数据包。我想阅读Message数据包,因为我想知道订阅者何时向发布者发送请求以获得订阅。

PacketTypeFilter filter = new  PacketTypeFilter(org.jivesoftware.smack.packet.Message.class);

    PacketListener myListener = new PacketListener(){

        @Override
        public void processPacket(Packet packet) {
            // TODO Auto-generated method stub

            Log.d("PACKET" , "READ");

            if(packet instanceof org.jivesoftware.smack.packet.Message){

                    org.jivesoftware.smack.packet.Message msg = (org.jivesoftware.smack.packet.Message) packet;

            Log.d("MY MESSAGE" , msg.toXML()+ "");


                    }
                }

            };

    cxmpp.addPacketListener(myListener, filter);

我想要的只是阅读传入的消息包

1 个答案:

答案 0 :(得分:1)

如果您拥有所有配置位并且pubsub组件正常工作,那么为了获得正常消息,您将执行以下操作:

ConnectionConfiguration config = new ConnectionConfiguration("ADDRESS",PORT); // service name, also known as XMPP domain of the target server. 
config.setServiceName(this.pubsubServiceAddress);
connection = new XMPPConnection(config);
connection.connect();

PacketFilter filter = new MessageTypeFilter(Message.Type.normal); 

connection.addPacketListener(new PacketListener() { 
    public void processPacket(Packet packet) {
    Message mes = (Message)packet;
        // do your stuff here
    }
}, filter);