我正在使用jivesoftware Smack SDk进行实时聊天功能.
为了创建连接,我使用以下代码,
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");
它的工作非常好.
现在问题是,我希望获得特定用户的在线状态或获取所有在线用户的列表.
我从堆栈溢出尝试了很多解决方案,但没有什么对我有用.
我试过的解决方案之一是,
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());
}
这会返回一个列表,但所有用户的状态为null.
请有人帮我准确解决方案.
谢谢
解决方法:
您可以使用Presence.Type.subscribe来了解(作为用户)另一个用户的状态:
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo('another_user@example.com');
connection.sendPacket(subscribe);
并且“another_user”应以同样的方式批准您的请求:
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo('another_user@example.com');
connection.sendPacket(subscribe);