Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate messages receiving in android smack 4.1

I have been trying to create an android chat app using smack 4.1. Message sending and receiving is working fine, but the problem is same message is getting several times with in the mXmppConnection.addAsyncStanzaListener.I don't know if i have missed to add something to the connection.

This is my connection class:

XMPPTCPConnectionConfiguration.Builder configBuilder = new XMPPTCPConnectionConfiguration.builder();

 configBuilder.setUsernameAndPassword(mUser, "password@123");
configBuilder.setPort(5555);
configBuilder.setServiceName("tvm.myname.com");
configBuilder.setDebuggerEnabled(true);                    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
try 
{
XMPPTCPConnection mConnection = new XMPPTCPConnection(configBuilder.build());
mConnection.connect();
mConnection.login();
} 
catch (SmackException e) 
{
} 

And this the code where message receives:

mXmppConnection.addAsyncStanzaListener(new StanzaListener() {
 @Override
 public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
 Message message = (Message)packet;  
  Log.i("XMPPClient", "****** message " + message);
  // code for handling message
}                  `enter code here`
},null);

The real problem is i am getting the message several times..the value of message is printing in the logs several time. Please help me....

like image 310
Nidheesh Avatar asked Feb 09 '23 18:02

Nidheesh


1 Answers

FINALLY GOT SOLUTION

The issue is not with the client but due to careless coding. I have been assigning single instance of connection object to a class variable and listener is adding to these reference object every time. So that leads to calling listener multiple times....The fix is done by adding listener to the singleton connection object.

like image 180
Nidheesh Avatar answered Feb 12 '23 09:02

Nidheesh