使用smack重新连接到xmpp服务器

使用smack重新连接到xmpp服务器

本文介绍了使用smack重新连接到xmpp服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个聊天应用程序,当互联网稳定时一切都可以正常工作.就像我可以发送和接收消息.但是当Internet断开一段时间后,xmpp将关闭连接.当互联网恢复时,我正在尝试重新连接到服务器.成功重新连接后,我得到 Client already logged in 的Stream错误,并且连接自动关闭.

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.

这是我的广播接收器代码,用于检查Internet连接.

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): [email protected], 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.

感谢:)

推荐答案

这是Smack中的一个已知错误: SMACK-725 .

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

请注意,我不建议在Android上使用ReconnectionManager.相反,我建议为移动环境实现自己的重新连接逻辑,并利用Android提供的信息(例如,监听CONNECTIVITY_CHANGED意图).

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).

这篇关于使用smack重新连接到xmpp服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:56