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

发布于 2024-11-17 21:26:23 字数 2377 浏览 2 评论 0原文

我目前正在大学开展一个项目,该项目是一个 Android 应用程序,旨在将数据传递到服务器。

为了做到这一点,我需要通过 XMPP 与服务器建立或多或少一致的连接。连接是否 100% 存在并不重要,但因为系统应该或多或少对用户不可见,所以用户交互应该是最少的。

服务器和客户端都是xmpp-clients。我使用 jabber.org 作为 xmpp 服务器。

我有一个 Android 服务正在建立与服务器的连接并传递数据,并且工作正常。

现在,我尝试在连接丢失或从 Wifi 更改为 GSM 时重新连接服务。我想尝试通过广播接收器监听NETWORK_STATE_CHANGED_ACTION来完成这项工作。但我什至没有走到这一步。

这就是问题所在:我尝试运行该应用程序,然后禁用我的 wifi。我的手机自动切换到 GSM,然后我失去了连接(这是我预料到的)。但是当我尝试手动重新连接(例如重新启动服务)时,我从服务器收到错误。而且我的状态仍然是“可用”。从那一刻起,我需要很长时间才能再次连接。

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

我实际上已连接到 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>

有时我没有收到错误,但仍然没有传递任何消息。

所以我认为服务器不允许我连接,因为我在尝试重新连接之前没有断开连接。我觉得这很奇怪,因为我认为你甚至可以从多个客户端连接到一个帐户。

我发布了一些我认为可能相关的代码:

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

这就是我发送消息的方式:

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

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

}

有人知道如何解决这个问题吗?只要我的消息被传递到服务器,我什至会使用“肮脏”的技巧。

顺便说一句:发件人和收件人的 jid 始终相同(初始设置后)。以防万一有人认为这很重要。

I'm currently working on a Project in my University which is an Android-App which is supposed to deliver data to a Server.

In order to do that I require a more or less consistent connection to the server via 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.

Both the server and the client are xmpp-clients. I use jabber.org as the xmpp server.

I have an Android-Service that is establishing the connection to server and delivers data and this works fine.

Now I tried to make the Service reconnect when connection is lost or is Changed from Wifi to GSM. I wanted to try to make this work with a broadcastreceiver listening to 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. My Phone than automatically switches to GSM and I lose my connection (which I anticipated). But when I try to reconnect manually (e.g. 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:

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). Just in case anyone thinks that could matter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2024-11-24 21:26:23

Smack 断开连接期间出现死锁

正如 Airsource Ltd. 在评论中提到的:Smack 在 disconnect() 中遇到死锁,记录为 SMACK-278。我提交了一个在我的 smack fork 中修复此问题的提交

Android 重新连接处理

对于网络故障转移,请查看 GTalkSMS 接收器。它将发出 ACTION_NETWORK_CHANGED 意图,并带有布尔值“available”和“fallover”。当“available=false”和“fallover=false”时,您的服务应该“停止”连接。如何“停止”连接取决于您。有时,即使修复了 SMACK-278,disconnect() 也会花费很长时间,这就是我们执行 在 x 秒后中止的线程中断开连接,然后创建一个新的 Connection 实例。当收到“available=true”的意图时,我们重新连接。

您可以在 GTalkSMS 源代码中找到其他示例。我让应用程序永久运行,它实现了稳定但不是 100% 可用的连接(因为 WLAN <-> GSM 交换机)。

Deadlock during Smack disconnect

As Airsource Ltd. mentioned in his comment: Smack is suffering from an deadlock in disconnect() which is loged as SMACK-278. I have made a commit that fixes this in my smack fork.

Android reconnection handling

For the network fallover, have a look at the GTalkSMS receiver. It will issue a ACTION_NETWORK_CHANGED intent, with the boolean extras "available" and "fallover". Your service should "stop" the connection when "available=false" and "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. We reconnect then when the intent with "available=true" is received.

You will find other examples in the GTalkSMS source. I have the app permanently running and it achieves a stable, but not 100% available connection (because of WLAN <-> GSM switches).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文