4

I'm using aSmack. My app listens a chatroom and reacts to the messages but it never send a message. The app doesn't receive more messages if the chatroom remains in silence for a while and then a new message is sent. I researched and I think that XEP-0199 is the solution here. I see that @Flow (the current Smack maintainer) implemented it and the issue related was closed.

I think that I need to use PingProvider but I really don't know how to connect this class with the Connection.

How can I enable the XEP-0199? How can I use PingProvider?


Connection code:

smack = SmackAndroid.init(getActivity().getApplicationContext());
connection = new XMPPConnection(App.getServer());
connection.addConnectionListener(new ConnectionListener() {

    private final static String SMACK = "SMACK";

    @Override
    public void reconnectionSuccessful() {
        Log.i(SMACK , "reconnectionSuccessful");
    }

    @Override
    public void reconnectionFailed(Exception e) {
        Log.i(SMACK, "reconnectionFailed", e);
    }

    @Override
    public void reconnectingIn(int seconds) {
        Log.i(SMACK, "reconnectingIn " + seconds);
    }

    @Override
    public void connectionClosedOnError(Exception e) {
        Log.i(SMACK, "connectionClosedOnError", e);
    }

    @Override
    public void connectionClosed() {
        Log.i(SMACK, "connectionClosed");
    }
});
connection.connect();
connection.login(user, password);
Brais Gabin
  • 5,827
  • 6
  • 57
  • 92

4 Answers4

6

I fix the problem implementing the ping response manually:

connection.addPacketListener(new PacketListener() {

    @Override
    public void processPacket(Packet packet) {
        connection.sendPacket(new Pong((Ping) packet));
    }
}, new PacketFilter() {

    @Override
    public boolean accept(Packet packet) {
        return packet instanceof Ping;
    }
});
Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
  • what is pong? can You explain? – Opiatefuchs Sep 12 '15 at 06:03
  • It seems that they remove the `Pong` class [in this commit](https://github.com/igniterealtime/Smack/commit/aab1dcdabee48b90b0d175fd99167afe92a28534). But now [`Ping` has a `getPong()` method](https://github.com/igniterealtime/Smack/blob/001e824fb9c1b3548991ea0185bd47f4ea03a087/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/packet/Ping.java#L43-L45), you can try with that. – Brais Gabin Sep 12 '15 at 21:28
  • How can we manage when user go offline i mean how we can stop ping manager manually after button pressed ? – Vishal Patoliya ツ May 24 '17 at 07:33
  • for receiving notification from FCM – Vishal Patoliya ツ May 24 '17 at 07:33
6

To prevent user from disconnecting your session

PingManager pm =  PingManager.getInstanceFor(MyApplication.connection) ;
    pm.setPingInterval(5) ;  // 5 sec
    pm.pingMyServer() ;
    pm.registerPingFailedListener(new PingFailedListener() {

        @Override
        public void pingFailed() {
            Log.e(TAG , "pingFailed") ;
        }
    });
Diaa Saada
  • 1,026
  • 1
  • 12
  • 22
  • I'd like to point out that a 5 second ping interval, if used on a mobile connection, will pretty fast drain the devices battery. – Flow May 19 '15 at 09:53
  • there is a big problem , if user became offline with out notifing openfire first , openfire will assume that the client got the packets so it wont be sent again when he logged in e.g. user A is online user B sent MUC invitation to A user A became offline before receiving the invitation without notifying openfire user B will assume A recived the invitation , and the invitation is lost – Diaa Saada May 20 '15 at 18:51
1

XEP 0199 is not a solution, Ping is used to check weather the server is up or not. actually you will send ping to the server.

Now as fas as your problem is concerned. Show me the message stanza that you are trying to send. and also check if the chat-room is public or private. you can not send a message to a private chat room.

Answer Updated:

Try using this code for detecting message recieve

PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        Network.connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message.getFrom());
                    Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
                   //recieve.setText(message.getBody());
                    /*messages.add(fromName + ":");
                    messages.add(message.getBody());*/
                    // Add the incoming message to the list view
                    item = new RowItem(R.drawable.billing, message.getBody());
                    adapter = new CustomListViewAdapter(getBaseContext(),
                            R.layout.list_item, rowItems);
                    rowItems.add(item);
                    //listView.setAdapter(adapter);
                }
            }
        }, filter);
Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55
  • I don't want to send any message. I only want to receive all the messages in a long period of time. But when the chatroom is in silent for a while it seems that I lose the conection but I don't receive any event through the `ConnectionListener`. In my tests the app is always in foreground and the screen turned on. – Brais Gabin May 30 '14 at 11:01
  • I just added it in my question. – Brais Gabin May 30 '14 at 11:17
0

I called PingManager.getInstanceFor method to enable XEP-0199 support.

user3640997
  • 91
  • 1
  • 7