I’m using the asmack XMPP with Android, but I have a problem. I would like a player to connect to XMPP server (only if username not already taken), receive all currently connected users and choose a user from the list to play against.
Currently I’m logging into the XMPP server like this:
/* then connect with a newly created username */
try {
if (connection != null && connection.isConnected()) {
connection.login(username, password);
}
} catch (XMPPException e) {
Log.w("[xmpp_login] Cannot connect to XMPP server with username: " + username, "0");
e.printStackTrace();
}
And for the creation of users I’m using the following code:
/* required attributes for creation of a new account */
HashMap<String, String> attr = new HashMap<String, String>();
attr.put("username", username);
attr.put("password", password);
manager.createAccount(username, password, attr);
} catch (XMPPException e) {
Log.w("[create_user] Cannot create new user: XMPP Exception.", "0");
Log.w(e.getMessage(), "0");
e.printStackTrace();
} catch (IllegalStateException e) {
Log.w("[create_user] Cannot create new user: not logged in.", "0");
e.printStackTrace();
}
I have two questions:
- How can I check if the username is already taken?
- How can I return a list of all currently connected (online) users to each player? Since I’ve googled and found nothing useful, I guess I’ll have to provide that function at serverside. This is where I’m using openfire XMPP server. Is there a plugin for it that I can use that does that – then I could call some function from a client, which would call that specific plugin, which will in turn respond with all the online players.
I guess any tip is appreciated. Thank you
I found a solution. To find an answer you can google “XMPP shared group”. Basically I’ve done the following:
Of course you’ll have to have the right modules enabled for those preferences to even be present.
Then we can just write the following function that will return all users from XMPP server: