When to disconnect a bosh connection from an application server to use pre-binding in a stanza?

This question is a continuation of my previous question on this SO question " How to connect the bmp XMPP server using the java smack library? "

I use Java as a server language. I successfully implemented the xmpp BOSH connection using smach-jbosh thanks to @Deuteu, helping me achieve this, so far I have modified the jbosh file BOSHClient.javaand added two getter methods to extract the RID and SID.

Now I have a RID and SID on my application server (I use Apache Tomcat). I need to pass these credentials to Strophe (web client) so that it can connect to the connection.

Here I have some doubts.

  • When to disconnect bosh Connection to install from the application server? before passing sid, get rid and drop to stanza or after passing sid, get rid and drop to stanza?
     According to my observation during the implementation for the same, I noticed that when the connection to the application server is disconnected, the session expired, and the SID and RID are no longer useful !!!
  • I implemented this logic (by setting up a bosh connection and extracting sid and rid) on the Servlet, here, when the response was sent from the Servlet, the stream will expire and the end of the BOSH connection will stop, so I cannot execute `Attach ()` on strophe after the expiration session actions.

Can someone help me with this problem?

+3
2

, BOSH smack SID + RID, Strophe attach() strophe . , Strophe , , .

- , , RID, .

, strophe, . , SID + RID , , - , .

, , XMPP TCP, BOSH ( BOSH -). . XMPP BOSH, , . , BOSH , XMPP, .

+2

, @fpsColton - . , : : , "DH"

BOSHConnection:

 // DH: function to preserve current api
public void login(String username, String password, String resource)
        throws XMPPException {
    login(username, password, resource, false);         

}

// DH: Most of this is existing login function, but added prebind parameter 
//     to allow leaving function after all required pre-bind steps done and before 
//     presence stanza gets sent (sent from attach in XMPP client)
public void login(String username, String password, String resource, boolean preBind)         
        throws XMPPException {
    if (!isConnected()) {
        throw new IllegalStateException("Not connected to server.");
    }
    if (authenticated) {
        throw new IllegalStateException("Already logged in to server.");
    }
    // Do partial version of nameprep on the username.
    username = username.toLowerCase().trim();

    String response;
    if (config.isSASLAuthenticationEnabled()
            && saslAuthentication.hasNonAnonymousAuthentication()) {
        // Authenticate using SASL
        if (password != null) {
            response = saslAuthentication.authenticate(username, password, resource);
        } else {
            response = saslAuthentication.authenticate(username, resource, config.getCallbackHandler());
        }
    } else {
        // Authenticate using Non-SASL
        response = new NonSASLAuthentication(this).authenticate(username, password, resource);
    }

    // Indicate that we're now authenticated.
    authenticated = true;
    anonymous = false;

    // DH: Prebind only requires connect and authenticate
    if (preBind) {
        return;
    }

    // Set the user.
    if (response != null) {
        this.user = response;
        // Update the serviceName with the one returned by the server
        config.setServiceName(StringUtils.parseServer(response));
    } else {
        this.user = username + "@" + getServiceName();
        if (resource != null) {
            this.user += "/" + resource;
        }
    }

    // Create the roster if it is not a reconnection.
    if (this.roster == null) {
        this.roster = new Roster(this);
    }
    if (config.isRosterLoadedAtLogin()) {
        this.roster.reload();
    }

    // Set presence to online.
    if (config.isSendPresence()) {
        sendPacket(new Presence(Presence.Type.available));
    }

    // Stores the autentication for future reconnection
    config.setLoginInfo(username, password, resource);

    // If debugging is enabled, change the the debug window title to include
    // the
    // name we are now logged-in as.l
    if (config.isDebuggerEnabled() && debugger != null) {
        debugger.userHasLogged(user);
    }
}

 // DH
@Override
public void disconnect() {
    client.close();
}

(-) - JSP:

. , , .

public class SmackBoshConnector {

private String sessionID = null;
private String authID = null;
private Long requestID = 0L;
private String packetID = null;
private boolean connected = false;

public boolean connect(String userName, String password, String host, int port, final String xmppService) {

    boolean success = false;

    try {

        Enumeration<SaslClientFactory> saslFacts = Sasl.getSaslClientFactories();
        if (!saslFacts.hasMoreElements()) {
            System.out.println("Sasl Provider not pre-loaded"); 
            int added = Security.addProvider(new com.sun.security.sasl.Provider()); 
            if (added == -1) {
                System.out.println("Sasl Provider could not be loaded");
                System.exit(added);
            }
            else {
                System.out.println("Sasl Provider added"); 
            }                                                      
        }

        BOSHConfiguration config = new BOSHConfiguration(false, host, port, "/http-bind/", xmppService);
        BOSHConnection connection = new BOSHConnection(config);      

        PacketListener sndListener = new PacketListener() {

            @Override
            public void processPacket(Packet packet) {
                SmackBoshConnector.this.packetID = packet.getPacketID();
                System.out.println("Send PacketId["+packetID+"] to["+packet.toXML()+"]");
            }

        };

        PacketListener rcvListener = new PacketListener() {

            @Override
            public void processPacket(Packet packet) {
                SmackBoshConnector.this.packetID = packet.getPacketID();
                System.out.println("Rcvd PacketId["+packetID+"] to["+packet.toXML()+"]");
            }

        };

        PacketFilter packetFilter = new PacketFilter() {

            @Override
            public boolean accept(Packet packet) {
                return true;
            }
        };

        connection.addPacketSendingListener(sndListener, packetFilter);
        connection.addPacketListener(rcvListener, packetFilter);
        connection.connect();

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

        authID = connection.getConnectionID();

        BOSHClient client = connection.getClient();

        sessionID = client.getSid();
        requestID = client.getRid();

        System.out.println("Connected ["+authID+"] sid["+sessionID+"] rid["+requestID+"]");
        success = true;
        connected = true;

        try {
            Thread.yield();
            Thread.sleep(500);
        }
        catch (InterruptedException e) {
            // Ignore
        }
        finally {
            connection.disconnect();
        }

    } catch (XMPPException ex) {
        Logger.getLogger(SmackBoshConnector.class.getName()).log(Level.SEVERE, null, ex);
    }

    return success;
}

public boolean isConnected() {
    return connected;
}

public String getSessionID() {
    return sessionID;
}

public String getAuthID() {
    return authID;
}

public String getRequestIDAsString() {
    return Long.toString(requestID);
}

public String getNextRequestIDAsString() {
    return Long.toString(requestID+1);
}
public static void main(String[] args)  {        
    SmackBoshConnector bc = new SmackBoshConnector();        
    bc.connect("dazed", "i3ji44mj7k2qt14djct0t5o709", "192.168.2.15", 5280, "my.xmppservice.com");
 }

}

, , Thread.yield Thread.sleep(1/2 ) - - PacketListener - - , , () , . , , @fpsColton, dicsonnect() .

: , sleep() yield(). , Smack sleep() , XMPPConnection.shutdown() source, yield() (Java Oracle Database - , ), - Smack Forum Thread.

.

+4

All Articles