简体   繁体   English

Android/XMPP:连接类型更改后无法重新连接到服务器

[英]Android/XMPP: Unable to reconnect to Server after Connection Type Changes

I'm currently working on a Project in my University which is an Android-App which is supposed to deliver data to a Server.我目前正在我大学的一个项目中工作,这是一个 Android 应用程序,应该将数据传递到服务器。

In order to do that I require a more or less consistent connection to the server via XMPP.为此,我需要通过 XMPP 与服务器建立或多或少一致的连接。 It is not really important that the connection is there 100% of the time, but it because the system is supposed to be more or less invisible to the user, user-interaction is supposed to be minimal. 100% 的时间都存在连接并不重要,但因为系统应该或多或少对用户不可见,所以用户交互应该是最小的。

Both the server and the client are xmpp-clients.服务器和客户端都是 xmpp-clients。 I use jabber.org as the xmpp server.我使用jabber.org作为 xmpp 服务器。

I have an Android-Service that is establishing the connection to server and delivers data and this works fine.我有一个 Android-Service 正在建立与服务器的连接并提供数据,这工作正常。

Now I tried to make the Service reconnect when connection is lost or is Changed from Wifi to GSM.现在,我尝试在连接丢失或从 Wifi 更改为 GSM 时重新连接服务。 I wanted to try to make this work with a broadcastreceiver listening to NETWORK_STATE_CHANGED_ACTION .我想尝试使用 broadcastreceiver 来完成这项工作,听NETWORK_STATE_CHANGED_ACTION But I did not even get this far.但我什至没有走到这一步。

This is the problem: I tried running the app and then just disabling my wifi.这就是问题所在:我尝试运行该应用程序,然后只是禁用了我的 wifi。 My Phone than automatically switches to GSM and I lose my connection (which I anticipated).我的手机会自动切换到 GSM,然后我失去了连接(这是我预料到的)。 But when I try to reconnect manually (eg restarting the service) I get errors from the Server.但是当我尝试手动重新连接(例如重新启动服务)时,我从服务器收到错误。 Also my status is still "available".此外,我的状态仍然是“可用”。 From that moment on it takes way too long until I can connect again.从那一刻起,我需要很长时间才能再次连接。

06-29 18:12:14.888: WARN/System.err(14246): resource-constraint(500)
06-29 18:12:14.890: WARN/System.err(14246):     at org.jivesoftware.smack.NonSASLAuthentication.authenticate(NonSASLAuthentication.java:110)
06-29 18:12:14.890: WARN/System.err(14246):     at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:404)
06-29 18:12:14.890: WARN/System.err(14246):     at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
....

I am actually connected to the xmpp Server but it does not deliver my message:我实际上已连接到 xmpp 服务器,但它没有传递我的消息:

06-29 18:12:14.882: INFO/System.out(14246): 06:12:14 nachm. RCV  (1079704816): <iq type='error' id='7rhk4-70'><error code='500' type='wait'><resource-constraint xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></iq>

Sometimes I don't get the error, but still no messages are being delivered.有时我没有收到错误消息,但仍然没有消息被传递。

So I think that the server is not allowing me to connect, because I did not disconnect before trying to reconnect.所以我认为服务器不允许我连接,因为我在尝试重新连接之前没有断开连接。 Which I find strange because I thought you could even connect from multiple clients to one account.我觉得很奇怪,因为我认为您甚至可以从多个客户连接到一个帐户。

I'm posting some code that I think might be relevant:我发布了一些我认为可能相关的代码:

public void connectToServer() throws XMPPException {

    ConnectionConfiguration config = new ConnectionConfiguration(
            serverADD, port,
            service);

    connection = new XMPPConnection(config);
    connection.connect();

    SASLAuthentication.supportSASLMechanism("PLAIN", 0);

    connection.login(user_JID,password);

    Presence presence = new Presence(Presence.Type.available);
            presence.setStatus("available");
            presence.setPriority(24);
    presence.setMode(Presence.Mode.available);
        connection.sendPacket(presence);
}

this is how I send the messages:这就是我发送消息的方式:

public void sendMessage(String message, String recipient) throws XMPPException {

    chat = connection.getChatManager().createChat(recipient, this);
    chat.sendMessage(message);

}

Does anyone hava an idea how to solve this?有谁知道如何解决这个问题? I would even use "dirty" tricks as long as my message gets delivered to the server.只要我的消息被传递到服务器,我什至会使用“肮脏”的技巧。

By the way: The senders and the recipients jids are always the same (after initial setup).顺便说一句:发件人和收件人的jids总是相同的(在初始设置之后)。 Just in case anyone thinks that could matter.以防万一有人认为这很重要。

Deadlock during Smack disconnect Smack 断开连接期间的死锁

As Airsource Ltd. mentioned in his comment: Smack is suffering from an deadlock in disconnect() which is loged as SMACK-278 .正如 Airsource Ltd. 在他的评论中提到的:Smack 在disconnect()中遇到死锁,记录为SMACK-278 I have m ade a commit that fixes this in my smack fork .我已经提交了在我的 smack fork 中修复此问题的提交

Android reconnection handling Android重连处理

For the network fallover, have a look at the GTalkSMS receiver .对于网络故障转移,请查看GTalkSMS 接收器 It will issue a ACTION_NETWORK_CHANGED intent, with the boolean extras "available" and "fallover".它将发出 ACTION_NETWORK_CHANGED 意图,其中 boolean 附加“可用”和“故障转移”。 Your service should "stop" the connection when "available=false" and "fallover=false".当“available=false”和“fallover=false”时,您的服务应该“停止”连接。 How you "stop" the connection is up to you.如何“停止”连接取决于您。 Sometimes the disconnect() takes very long even with the fix for SMACK-278, this is why we do the disconnect in an thread that will abort after x seconds and then creates a new Connection instance.有时即使修复了 SMACK-278,disconnect() 也需要很长时间,这就是为什么我们在一个线程中断开连接,该线程将在 x 秒后中止,然后创建一个新的 Connection 实例。 We reconnect then when the intent with "available=true" is received.当收到带有“available=true”的意图时,我们重新连接。

You will find other examples in the GTalkSMS source.您将在 GTalkSMS 源代码中找到其他示例。 I have the app permanently running and it achieves a stable, but not 100% available connection (because of WLAN <-> GSM switches).我让应用程序永久运行,它实现了稳定但不是 100% 可用的连接(因为 WLAN <-> GSM 开关)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM