我设法通过在 Smack BOSHConnection 库类中添加以下函数来做到这一点:
public class BOSHConnection extends Connection {
...
public String getSid() {
return client.getSid();
}
public Long getRid() {
return client.getRid();
}
...
}
然后,您需要记住 Rid 是 Smack 库使用的最后一个 id,对于您的预绑定,您需要为下一个请求增加它。
Jid 已经可以通过 BOSHConnection.getUser();
我还应该指出,要让 converse pre-bind 与 Smack 一起工作,我还必须更改 BOSHConnection.login 函数。
// Changed: preserve current api - call new pre-bind aware function
public void login(String username, String password, String resource)
throws XMPPException {
login(username, password, resource, false);
}
// Added: Using original login function with prebind awareness
public void login(String username, String password, String resource, boolean preBind)
throws XMPPException {
if (!isConnected()) {
throw new IllegalStateException("Not connected to server.");
}
... unchanged
// Indicate that we're now authenticated.
authenticated = true;
anonymous = false;
// Added: Prebind only requires connect and authenticate
if (preBind) {
return;
}
然后在网络应用中
// login with pre-bind only
connection.login(userName, password, "", true);
这是必需的,因为 converse 所做的第一件事是执行登录功能后半部分所做的所有花名册和出席信息。我的理由是 XMPP 服务器实际上会看到两个连接(一个来自 SMACK,一个来自 converse),它是发送出席信息的连接,最终将成为 XMPP 消息的目标 - 我们希望它是相反的。
编辑:更长的代码示例是in this other StackOverflow answer