Reconnection to xmpp server using smack

自闭症网瘾萝莉.ら 提交于 2019-12-01 08:26:40

问题


i am trying to develop a chat app, everything is working fine when internet is stable. like i can send and receive messages. but when the internet goes off for a while xmpp closes the connection. when internet comes back i am trying to reconnect to server. after successful reconnection i got the Stream error of Client already logged in and connection is automatically closed.

Here is my Broadcast receiver code to check Internet connection.

networkReceiver = new BroadcastReceiver (){
        @Override
        public void onReceive(Context context, Intent intent) {
            //super.onReceive(context, intent);
            if(intent.getExtras()!=null) {
                NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
                if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
                    enableChat();
                }
            }
           else if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
                disableChat();
            }
            else{
                disableChat();
            }
        }

    };

here is the code for enabling chat and disabling chat.

public static void disableChat() {

    msg_edittext.setClickable(false);
    sendButton.setClickable(false);
    form.setClickable(false);

    msg_edittext.setEnabled(false);
    sendButton.setEnabled(false);
    form.setEnabled(false);
    showNoInternetView();
    if (xmpp.getConnection()!=null)
    xmpp.getConnection().disconnect();

}

public static void showNoInternetView() {
    linrNoInternet.setVisibility(View.VISIBLE);
    msgListView.setVisibility(View.GONE);
}

public static void showInternetView() {
    linrNoInternet.setVisibility(View.GONE);
    msgListView.setVisibility(View.VISIBLE);
}
    public static void enableChat() {
    msg_edittext.setClickable(true);
    sendButton.setClickable(true);
    form.setClickable(true);

    showInternetView();

    msg_edittext.setEnabled(true);
    sendButton.setEnabled(true);
    form.setEnabled(true);
    xmpp.connect("reconnect..");

}

while trying to reconnect the error log it shows is..

07-04 10:25:52.880 8581-8612/com.vario.community D/xmpp: Authenticated!
07-04 10:25:52.880 8581-9732/com.vario.community D/SMACK: SENT (0): <iq     id='BpGkK-5' type='get'><query xmlns='jabber:iq:roster'></query></iq>
07-04 10:25:52.882 8581-8612/com.vario.community D/SMACK: XMPPConnection  authenticated (0)
07-04 10:25:52.882 8581-8612/com.vario.community I/LOGIN: Yey! We're connected to the Xmpp server!
07-04 10:25:52.883 8581-8612/com.vario.community D/SMACK: XMPPConnection connected (0)
07-04 10:25:52.883 8581-9732/com.vario.community D/SMACK: SENT (0):  <presence id='BpGkK-6'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1'  node='http://www.igniterealtime.org/projects/smack'  ver='E6FpoDPmjMQ3i53V+HWh0YBrS7U='/></presence>
07-04 10:25:52.884 8581-9732/com.vario.community D/SMACK: SENT (0): <iq id='BpGkK-18' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></bind></iq>
07-04 10:25:52.885 8581-9732/com.vario.community D/SMACK: SENT (0): <r xmlns='urn:xmpp:sm:3'/>
07-04 10:25:52.886 8581-9732/com.vario.community D/SMACK: SENT (0): <iq id='BpGkK-20' type='get'><query xmlns='jabber:iq:roster'></query></iq>
07-04 10:25:52.886 8581-8612/com.vario.community E/(reconnect..): SMACKException: Client is already logged in
07-04 10:25:52.888 8581-9732/com.vario.community D/SMACK: SENT (0): <presence id='BpGkK-21'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='JcQq0xyVq6PbVQSN9PSQq58gxiQ='/></presence>
07-04 10:25:52.890 8581-8581/com.vario.community V/TextView: stopSelectionActionMode()
07-04 10:25:52.893 8581-9732/com.vario.community D/SMACK: SENT (0): <presence id='BpGkK-22' type='unavailable'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='JcQq0xyVq6PbVQSN9PSQq58gxiQ='/></presence>
07-04 10:25:52.893 8581-9732/com.vario.community D/SMACK: SENT (0): <a xmlns='urn:xmpp:sm:3' h='0'/>
07-04 10:25:52.894 8581-9732/com.vario.community D/SMACK: SENT (0): </stream:stream>

07-04 10:25:57.935 8581-9032/com.vario.community E/Roster: Exception reloading roster
                                                       org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: IQReplyFilter: iqAndIdFilter (AndFilter: (OrFilter: (IQTypeFilter: type=error, IQTypeFilter: type=result), StanzaIdFilter: id=BpGkK-20)), : fromFilter (OrFilter: (FromMatchesFilter (full): null, FromMatchesFilter (bare): darshan350@vario.fitness, FromMatchesFilter (full): vario.fitness)).
                                                           at org.jivesoftware.smack.AbstractXMPPConnection$6.run(AbstractXMPPConnection.java:1443)
                                                           at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                           at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
                                                           at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                           at java.lang.Thread.run(Thread.java:818)

So, if anyone knows the process of proper reconnection please help me. i have searched for this question but i haven't got the solution yet.

thanks :)


回答1:


That is a known bug in Smack: SMACK-725.

Note that I don't recommend using ReconnectionManager on Android. Instead I suggest to implement your own reconnection logic tailed for a mobile environment and exploiting the information Android provides (e.g. listening for the CONNECTIVITY_CHANGED intent).



来源:https://stackoverflow.com/questions/38177266/reconnection-to-xmpp-server-using-smack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!