Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

xmpp - Java - trying to prebind converse.js using bosh but not able to get the sid and rid...using the smack bosh

I am trying to connect to the open fire using the smack bosh and trying to prebind the converse.js in the web page. My bosh code

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) {
}

Can some one help me how to establish a bosh connection and get the required sid, rid and jid from the session....

Any help is much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I managed to do this by adding the following functions in the Smack BOSHConnection library class:

public class BOSHConnection extends Connection {
    ...
    public String getSid() {
        return client.getSid();
    }

    public Long getRid() {
        return client.getRid();
    }
    ...
}

you then need to remember that the Rid is the last id used by the Smack library and for your pre-bind you need to increment this for next request.

Jid is already available via BOSHConnection.getUser();

I should also flag that to get converse pre-bind working with Smack I also had to change the BOSHConnection.login functon.

// 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;
    }

then in Web app

// login with pre-bind only
connection.login(userName, password, "", true);

This is required as the first thing converse does is perform all the roster and presence stuff that the later half of the login function does. My reasoning here is that the XMPP server will actually see two connections (one from SMACK and one from converse) and it's the one that sends the presence which will end up being target for XMPP messages - and we want that to be converse.

Edit: Longer code sample for this is in this other StackOverflow answer


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...