Reputation: 2712
I am working with jivesoftware Smack SDk for real time chatting functionality. For creating connection I am using following code ,
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setServiceName("world-pc");
config.setHost(serverAddress);
config.setPort(5222);
config.setDebuggerEnabled(true);
XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);
connection = new XMPPTCPConnection(config.build());
XMPPConnectionListener connectionListener = new XMPPConnectionListener();
connection.addConnectionListener(connectionListener);
connection.connect();
connection.login("username","password");
And its working amazingly fine. Now the thing is that, I want to get online status of particular user or to get list of all users who are online. I have tried many solutions from stack overflow, but nothing works for me. One of my solution which I tried is,
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
Roster roster = xmppConnection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
Presence presence;
for(RosterEntry entry : entries) {
presence = roster.getPresence(entry.getUser());
System.out.println(entry.getUser());
System.out.println(presence.getType().name());
System.out.println(presence.getStatus());
}
This returns me a list, but the status is null for all users. Please some one help me with accurate solution.
Thank you
Upvotes: 3
Views: 1766
Reputation: 502
You could use Presence.Type.subscribe
in order to know (being a user), the status of another user:
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo('[email protected]');
connection.sendPacket(subscribe);
And the "another_user" should approves your request in the same way:
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo('[email protected]');
connection.sendPacket(subscribe);
Upvotes: 3
Reputation: 2940
Presence it's made by a TYPE
(like: Presence.Type.available
or Presence.Type.unavailable
) and a custom nullable status
from User (like "Hello World!" or "Today I'm happy" or "At Work right now").
To set status, simply set it before send:
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Online and ready to chat");
connection.sendStanza(presence); //or old one: connection.sendPacket(presence)
Upvotes: 2