1
votes

I'm using aSmack and Openfire for my chat application.

I've wrote background service which listens to internet connection and connects to XMPP server.

When service try to connect XMPP server, it is throwing 'Already Logged in to server' exception.

I've set Resource Policy as 'Always kicked' in server setting & tried to pass 'null' as third parameter in login() method, but none of them are working for me. Can anyone help me out?

04-11 18:09:50.293: E/AndroidRuntime(25422): FATAL EXCEPTION: Thread-2212
04-11 18:09:50.293: E/AndroidRuntime(25422): Process: com.example.singlechat:remote, PID: 25422
04-11 18:09:50.293: E/AndroidRuntime(25422): java.lang.IllegalStateException: Already logged in to server.
04-11 18:09:50.293: E/AndroidRuntime(25422):    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:232)
04-11 18:09:50.293: E/AndroidRuntime(25422):    at org.jivesoftware.smack.Connection.login(Connection.java:371)
04-11 18:09:50.293: E/AndroidRuntime(25422):    at com.example.singlechat.BackgroundService$3.run(BackgroundService.java:173)
04-11 18:09:50.293: E/AndroidRuntime(25422):    at java.lang.Thread.run(Thread.java:818)

Connection code:

// Connect XMPP Server
public void connectXMPPServer() {

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {

            // Create a connection
            ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    HOST, PORT, SERVICE);
            connConfig.setReconnectionAllowed(true);

            xmppConnection = new XMPPConnection(connConfig);

            try {

                xmppConnection.connect();
                Log.i("XMPPChatDemoActivity", "Connected to "
                        + xmppConnection.getHost());
            } catch (XMPPException ex) {

                Log.e("XMPPChatDemoActivity", "Failed to connect to "
                        + xmppConnection.getHost());
                Log.e("XMPPChatDemoActivity", ex.toString());
            }

            try {

                if (xmppConnection.isConnected()) {

                    xmppConnection.login(USERNAME, PASSWORD);
                    Log.i("XMPPChatDemoActivity", "Logged in as "
                            + xmppConnection.getUser());

                    Presence presence = new Presence(
                            Presence.Type.available);
                    xmppConnection.sendPacket(presence);
                }
            } catch (XMPPException ex) {

                Log.e("XMPPChatDemoActivity", "Failed to log in as "
                        + USERNAME);
                Log.e("XMPPChatDemoActivity", ex.toString());
            }
        }
    });
    t.start();
}

// Broadcast receiver; listens to network state
public BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        try {

            if (isNetworkOn()) {

                Log.i("BackgroundService-BroadcastReceiver",
                        "Network is on");
                connectXMPPServer();
            } else if (!isNetworkOn()) {

                Log.i("BackgroundService-BroadcastReceiver",
                        "Network is off");
                xmppConnection.disconnect();
            }
        } catch (Exception e) {

            Log.e("BackgroundService-networkStateReceiver", e.toString());
        }
    }
};

// Returns network state
public boolean isNetworkOn() {

    ConnectivityManager connMngr = (ConnectivityManager) getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connMngr.getActiveNetworkInfo();
    return (netInfo != null && netInfo.isConnected());
}
2
did you solve this problem?Vilas
@EagleEye sorry. i'm still stuck on this.Sushant
Which aSmack version do you use? I recommend updating to the latest released Smack version (which is 4.1.5 at the time of writing this).Flow
@Flow it's 19-0.8.10Sushant

2 Answers

0
votes

There are pretty much customization options are given by Openfire like Kick resources after logged out etc. But none of them worked for me. So try-catch was the remaining option. If anyone does better research, their suggestion will be appreciated.

try {

    xmppConnection.login(USERNAME, PASSWORD);
} catch (IllegalStateException e) {

    Log.e("MessagingService", "Already Logged in as " + xmppConnection.getUser());
}
Log.i("MessagingService", "Logged in as " + xmppConnection.getUser());
-1
votes

This is an issue with the Smack library - XMPP Presence stanza is always send regardless of settings when the extensions are enabled.

See developer thread: https://community.igniterealtime.org/thread/55778

You can verify this by removing the Smack extensions dependency from app/build.gradle. In case you no longer have the exception then it means that the Smack developer should provide a solution.