我正在尝试使用 smack bosh 连接到明火,并尝试在网页中预绑定(bind) converse.js。我的波什代码
BOSHConfiguration config = new BOSHConfiguration(false, "host", 7070, "/http-bind/", "<host>", "xmpp:127.0.0.1:5222");
BOSHConnection connection = new BOSHConnection(config);
try {
connection.connect();
connection.login("un", "pw");
String sss = connection.getConnectionID();
String bosh = connection.BOSH_URI;
} catch (Exception e) {
}
有人可以帮助我如何建立 bosh 连接并从 session 中获取所需的 sid、rid 和 jid....
非常感谢任何帮助。
最佳答案
我设法通过在 Smack BOSHConnection 库类中添加以下函数来做到这一点:
public class BOSHConnection extends Connection {
...
public String getSid() {
return client.getSid();
}
public Long getRid() {
return client.getRid();
}
...
}
然后,您需要记住 Rid 是 Smack 库使用的最后一个 ID,对于预绑定(bind),您需要在下一个请求时增加此 ID。
Jid 已可通过 BOSHConnection.getUser() 使用;
我还应该指出,为了与 Smack 一起进行反向预绑定(bind),我还必须更改 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 消息的目标 - 我们希望它是 converse。
编辑:更长的代码示例是 in this other StackOverflow answer
关于Java - 尝试使用 bosh 预绑定(bind) converse.js 但无法获取 sid 并摆脱...使用 smack bosh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23264662/