无法使用 Smack 更改用户的状态

问题描述 投票:0回答:2

我尝试设置在线模式,但它无法通过名册工作。我运行此代码并检查我的本地主机服务器,模式仍然“可用”而不是“请勿打扰”。

final Connection connection = new XMPPConnection("xxx.xxx.x.xx");

connection.connect();
connection.login("hieugioi@hieund", "123456");

final Roster roster = connection.getRoster();           
Presence p = roster.getPresence("hieugioi@hieund");
p.setPriority(128);
p.setMode(Mode.dnd);
xmpp smack
2个回答
22
投票

因为你没有将 Presence 数据包发送到服务器。组装好状态数据包后,您需要发送它。例如:

Presence p = new Presence(available, "I am busy", 42, Mode.dnd);
connection.sendStanza(p);

另请参阅:Smack - 入门; “读写数据包”部分


0
投票

使用 Smack 4.4.4 和 Kotlin,您可以通过这种方式设置状态(仅在断开连接时显示不可用):

if (connection != null){
    var pb = connection!!.stanzaFactory.buildPresenceStanza()
      .ofType(Presence.Type.unavailable)
      .setMode(Presence.Mode.away)
    connection!!.sendStanza(pb.build())
    connection!!.disconnect()
}
© www.soinside.com 2019 - 2024. All rights reserved.