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

问题描述:

我目前正在在我大学的一个项目,该项目是一个Android的应用程序,它应该将数据传送到服务器。

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

为了做到这一点我需要通过XMPP或多或少一致的连接到服务器。它不是真正重要的是,连接是有的100%的时间,但它由于系统应该是或多或少对用户不可见的,用户交互被认为是最小的。

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.

两者服务器和客户端是XMPP协议客户端。我用 jabber.org 作为XMPP服务器。

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

我有一个Android的服务是建立连接到服务器,并提供数据,并能正常工作。

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

现在我试图使该服务重新连接时丢失连接或从无线更改为GSM。我想尝试,使这项工作有一个BroadcastReceiver听 NETWORK_STATE_CHANGED_ACTION 。但是,我甚至没有走到这一步。

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.

这就是问题所在:我试图运行该应用程序,然后就禁止我的WiFi。我的手机比会自动切换到GSM和我失去了我的连接(我的预期)。但是,当我尝试手动重新连接(如重新启动服务),我从服务器获取错误。还有我的状态仍然是可以。从它的那一刻花费的时间太长了,直到我可以再次连接。

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

我其实连到XMPP服务器,但它并没有提供我的消息:

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.

我张贴一些code,我认为可能是相关的:

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

这是我发送消息:

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.

顺便说一句:在发送者和接受者的JIDs始终是相同的(初始设置后)。以防万一有人认为,可能无所谓。

By the way: The senders and the recipients jids are always the same (after initial setup). Just in case anyone thinks that could matter.

由于Airsource有限公司在提到他的评论:拍击是从僵局痛苦断开连接()这是loged作为的 SMACK-278 。我有m个阿德提交了解决这个在我嫌叉

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.

有关网络fallover,看看在 GTalkSMS接收。它会发出ACTION_NETWORK_CHANGED的意图,与布尔额外的可用和fallover。您的服务应该停止连接时,可用=假和fallover =假。你如何停止的连接是由你。有时断开()需要很长的甚至与SMACK-278的修正,这就是为什么我们做的中,将中止后x秒的,然后一个线程断开连接创建一个新的连接实例。我们重新再收到意图以服务=真的时候。

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.

您会发现在GTalkSMS源的其他例子。我有该应用永久运行,并能实现稳定的,但不是100%有效连接(因为WLAN&所述的; - >的GSM交换机)。

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