AnsweredAssumed Answered

smack return last recieved message every time user get online

Question asked by sadegh on May 9, 2017
Latest reply on May 10, 2017 by sadegh

hi guys!

i start openFire on server everything is fine but when i send message to user that connect from Smack , he received last message every time he loged in.

i trace events i fund this:

when smack user close app and disconnected , last message he received insert to offline table of openfire server.

it seems smack not send delivery to server.

the code is blow:

 

 

class xmppConnection implements ConnectionListener {

 

   private final Context mApplicationContext;
  private final String mUsername;
  private final String mPassword;
  private final String mServiceName;
  private final String serverAddress;
  private AbstractXMPPConnection mConnection;
  static ConnectionState sConnectionState;
  private ChatManager chatManager;

  static enum ConnectionState {

   CONNECTED, AUTHENTICATED, CONNECTING, DISCONNECTING, DISCONNECTED
   }

 

   xmppConnection(Context context) {

   mApplicationContext = context;
   mUsername = "3";
   mPassword = "09332672100";
   mServiceName = "speedserver";
   serverAddress = "192.168.10.22";
   }

 

   void login() {

 

   try {

 

   if (mConnection == null || sConnectionState != ConnectionState.CONNECTED)

   return;

   mConnection.login();
   ReconnectionManager recM = ReconnectionManager.getInstanceFor(mConnection);
   ReconnectionManager.setEnabledPerDefault(true);
   recM.enableAutomaticReconnection();

  if (mConnection.isAuthenticated()) {

  Log.w("chatApp", "logedIn");
   }

 

  } catch (XMPPException | SmackException | IOException | InterruptedException e) {

  e.printStackTrace();
   }

  }

 

   void sendMessage(String message, String username) {

 

   if (sConnectionState != ConnectionState.AUTHENTICATED)

   return;
   EntityBareJid jid;
  try {

  jid = JidCreate.entityBareFrom(username);
   Chat chat = chatManager.createChat(jid);
   chat.sendMessage(message);
   Log.d("chatApp", "message Send");
   } catch (SmackException.NotConnectedException | XmppStringprepException | InterruptedException e) {

  e.printStackTrace();
   }

  }

 

   void recieveMessage() {

 

   chatManager.addChatListener(new ChatManagerListener() {

   @Override
   public void chatCreated(Chat chat, boolean b) {

  chat.addMessageListener(new ChatMessageListener() {

   @Override
   public void processMessage(Chat chat,
   org.jivesoftware.smack.packet.Message message) {

   //if (message.getTo().toString().startsWith("admin@hairedresser.chatserver"))
   chatService.RecieveMessage(chat, message);
   }

  });
   }

  });
   }

 

   void connect() {

 

   try {

  InetAddress addr = InetAddress.getByName(serverAddress);
   HostnameVerifier verifier = new HostnameVerifier() {

   @Override
   public boolean verify(String hostname, SSLSession session) {

   return false;
   }

  };
   DomainBareJid serviceName = JidCreate.domainBareFrom(mServiceName);

   XMPPTCPConnectionConfiguration config;

   config = XMPPTCPConnectionConfiguration.builder()

  .setUsernameAndPassword(mUsername, mPassword)

  .setXmppDomain(serviceName)

  .setHostnameVerifier(verifier)

  .setHostAddress(addr)

  .setDebuggerEnabled(true)

  .setResource("AndroidSmack")

  .setCompressionEnabled(true)

  .setHost(serverAddress)

  .setPort(5222)

  .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)

  .build();

   XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
   XMPPTCPConnection.setUseStreamManagementDefault(true);
   mConnection = new XMPPTCPConnection(config);
   mConnection.addConnectionListener(this);
   mConnection.setPacketReplyTimeout(60000);
   mConnection.connect();

  if (!mConnection.isConnected()) {

   sConnectionState = ConnectionState.DISCONNECTED;

  return;
   }

 

  Log.w("chatApp", "connected");

   chatManager = ChatManager.getInstanceFor(mConnection);

   } catch (SmackException | IOException | XMPPException | InterruptedException e) {

  e.printStackTrace();
   }

  }

 

   public void disconnect() {

  Log.d(TAG, "Disconnecting from serser " + mServiceName);
  try {

   if (mConnection != null) {

   mConnection.disconnect();
   }

 

  } catch (Exception e) {

  xmppConnection.sConnectionState = ConnectionState.DISCONNECTED;
   e.printStackTrace();
   }

   mConnection = null;
   }

 

   public void createNewUser() {

 

  Localpart lp;
  try {

 

  lp = Localpart.from("test3");
   Map<String, String> attributes = new HashMap<>();
   attributes.put("name", "full_name");
   attributes.put("email", "ssss@ddd.com");
   AccountManager accountManager = AccountManager.getInstance(mConnection);
   accountManager.sensitiveOperationOverInsecureConnection(true);
   accountManager.createAccount(lp, "mathan123", attributes);
   Log.d("chatapp","accCreateSeuc");

   } catch (XmppStringprepException | XMPPException.XMPPErrorException |

  SmackException.NotConnectedException | SmackException.NoResponseException |

  InterruptedException e) {

  e.printStackTrace();
   }

   // Registering the user

   }

 

   @Override
   public void connected(XMPPConnection connection) {

  xmppConnection.sConnectionState = ConnectionState.CONNECTED;
   }

 

   @Override
   public void authenticated(XMPPConnection connection, boolean resumed) {

  xmppConnection.sConnectionState = ConnectionState.AUTHENTICATED;
   }

 

   @Override
   public void connectionClosed() {

  xmppConnection.sConnectionState = ConnectionState.DISCONNECTED;
   }

 

   @Override
   public void connectionClosedOnError(Exception e) {

  xmppConnection.sConnectionState = ConnectionState.DISCONNECTED;
   }

 

   @Override
   public void reconnectionSuccessful() {

  xmppConnection.sConnectionState = ConnectionState.CONNECTED;
   }

 

   @Override
   public void reconnectingIn(int seconds) {

  xmppConnection.sConnectionState = ConnectionState.CONNECTING;
   }

 

   @Override
   public void reconnectionFailed(Exception e) {

  xmppConnection.sConnectionState = ConnectionState.DISCONNECTED;
   }

}

Outcomes