gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:18:39 30 4
gpt4 key购买 nike

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

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

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();
}
}

};

这里是启用聊天和禁用聊天的代码。

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

}

在尝试重新连接时显示的错误日志是..

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)

所以,如果有人知道正确重新连接的过程,请帮助我。我已经搜索过这个问题,但我还没有找到解决方案。

谢谢 :)

最佳答案

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

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

关于android - 使用 smack 重新连接到 xmpp 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177266/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com