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 (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:
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.
As Airsource Ltd. mentioned in his comment: Smack is suffering from an deadlock in disconnect()
which is loged as SMACK-278 . I have m ade a commit that fixes this in my smack fork .
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.