Project

General

Profile

how to use jaxmpp3.2.0 api

lin jiafu
Added over 1 year ago

i hava a simple code like this :

import tigase.jaxmpp.core.client.AsyncCallback;

import tigase.jaxmpp.core.client.JID;

import tigase.jaxmpp.core.client.XMPPException.ErrorCondition;

import tigase.jaxmpp.core.client.exceptions.JaxmppException;

import tigase.jaxmpp.core.client.xml.Element;

import tigase.jaxmpp.core.client.xml.ElementFactory;

import tigase.jaxmpp.core.client.xmpp.stanzas.IQ;

import tigase.jaxmpp.core.client.xmpp.stanzas.Stanza;

import tigase.jaxmpp.core.client.xmpp.stanzas.StanzaType;

import tigase.jaxmpp.j2se.ConnectionConfiguration.ConnectionType;

import tigase.jaxmpp.j2se.Jaxmpp;

public class Test {

public static void main(String[] args) {

    final Jaxmpp jaxmpp = new Jaxmpp();

jaxmpp.getConnectionConfiguration().setConnectionType(ConnectionType.bosh);

    jaxmpp.getConnectionConfiguration().setBoshService("http://192.168.18.45:5280");

// jaxmpp.getConnectionConfiguration().setDomain("hsl.com");

// jaxmpp.getConnectionConfiguration().setPort(5280);

    jaxmpp.getConnectionConfiguration().setUserJID("test@hsl.com");

    jaxmpp.getConnectionConfiguration().setUserPassword("test");

try {

        System.out.println("Loging in...");

        jaxmpp.login(true);

IQ stanza = IQ.createIQ();

        stanza.setId("rr82a1z7");

        stanza.setFrom(JID.jidInstance("test@hsl.com"));

        stanza.setTo(JID.jidInstance("admin@hsl.com"));

        stanza.setType(StanzaType.get);

        Element element = ElementFactory.create("query", "", "jabber:iq:roster");

        stanza.addChild(element);

        System.out.println(stanza.getAsString());

jaxmpp.send(stanza, new AsyncCallback() {

            @Override

            public void onTimeout() throws JaxmppException {

                // TODO Auto-generated method stub

                System.out.println("timeout...");

            }

@Override

            public void onSuccess(Stanza responseStanza) throws JaxmppException {

                // TODO Auto-generated method stub

                System.out.println("success...");

                System.out.println(responseStanza.getAsString());

            }

@Override

            public void onError(Stanza responseStanza, ErrorCondition error)

                    throws JaxmppException {

                System.out.println("error...");

                System.out.println(responseStanza.getAsString());

                // TODO Auto-generated method stub

            }

        });

System.out.println("Disconnecting...");

        jaxmpp.disconnect();

        System.out.println("END.");

    } catch (final JaxmppException e) {

      e.printStackTrace();

    }

}

}

but it does not work ,and it throw an exception:

tigase.jaxmpp.core.client.exceptions.JaxmppException: bosh#url is not set!

at tigase.jaxmpp.j2se.connectors.bosh.BoshWorker.run(BoshWorker.java:98)

at java.lang.Thread.run(Thread.java:748)

and i have no idea to how to use jaxmpp3.2.0,what i have seen in https://projects.tigase.org/projects/jaxmpp2/wiki/Example_codes is not suit to jaxmpp3.2.0

would you have demo about jaxmpp3.2.0 to me, thanks!


Replies (3)

Avatar?id=6098&size=32x32

Added by Bartosz Małkowski TigaseTeam over 1 year ago

Here is simple example:

public class SimpleConnect {

    public static void main(String[] args) throws Exception {
        Logger logger = Logger.getLogger("tigase.jaxmpp");
        Handler handler = new ConsoleHandler();
        handler.setLevel(Level.ALL);
        logger.addHandler(handler);
        logger.setLevel(Level.ALL);

        final Jaxmpp jaxmpp = new Jaxmpp();

        PresenceModule.setPresenceStore(jaxmpp.getSessionObject(), new J2SEPresenceStore());
        jaxmpp.getModulesManager().register(new PresenceModule());
        jaxmpp.getModulesManager().register(new CapabilitiesModule());
        jaxmpp.getModulesManager().register(new RosterModule());

        // ***

        jaxmpp.getConnectionConfiguration().setUserJID("test@hsl.com");
        jaxmpp.getConnectionConfiguration().setUserPassword("test");

        jaxmpp.getConnectionConfiguration().setConnectionType(ConnectionConfiguration.ConnectionType.bosh);
        jaxmpp.getConnectionConfiguration().setBoshService("http://192.168.18.45:5280");

        // ***

        // skip certificate check
        jaxmpp.getProperties()
                .setUserProperty(SocketConnector.TRUST_MANAGERS_KEY, new TrustManager[]{new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                            throws CertificateException {

                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
                            throws CertificateException {

                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }
                }});
        // skip certificate domain check
        jaxmpp.getSessionObject().setProperty(SocketConnector.HOSTNAME_VERIFIER_KEY, new HostnameVerifier() {
            public boolean verify(String s, SSLSession sslSession) {
                System.out.println(s + " is OK ;-) ");
                return true;
            }
        });

        // listening for events:
        jaxmpp.getEventBus()
                .addHandler(PresenceModule.ContactAvailableHandler.ContactAvailableEvent.class,
                            new PresenceModule.ContactAvailableHandler() {
                                @Override
                                public void onContactAvailable(SessionObject sessionObject, Presence presence, JID jid,
                                                               Presence.Show show, String s, Integer integer)
                                        throws JaxmppException {
                                    System.out.println("CONTACT " + jid + " is available");
                                }
                            });

        System.out.println("GO and wait");
        System.out.println("===========");
        jaxmpp.login(true);
        System.out.println("Done: login(true)");
        System.out.println("=================");

        Thread.sleep(5000);

        // sample of sending stanza
        Message msg = Message.create();
        msg.setTo(JID.jidInstance("admin@hsl.com"));
        msg.setBody("test");
        jaxmpp.send(msg);

        // using roster module
        List<RosterItem> roster = jaxmpp.getModule(RosterModule.class).getRosterStore().getAll();

        Thread.sleep(12000);

        jaxmpp.disconnect(true);

    }

}

(1)
Avatar?id=6098&size=32x32

Added by Bartosz Małkowski TigaseTeam over 1 year ago

Remember, that JaXMPP is async event based library, so (for example) jaxmpp.getModule(RosterModule.class).getRosterStore().getAll(); may return empty list because roster is not received by client yet. This is the reason why I added Threa.sleep() in this example.

(1)

Added by lin jiafu over 1 year ago

thanks for reply my question, i hava an other question,and i search it in google, but it not solve yet, the question is like the page describe: https://stackoverflow.com/questions/25412848/can-strophe-be-used-to-make-an-bosh-connection-with-bosh-manager-on-javajvm, i user strophe.js to connect to tigase in web im, but the function connection.attach(jid,sid,rid,callback) does not work, i guess is my sid and rid got wrong, this is my code :

import tigase.jaxmpp.core.client.JaxmppCore.LoggedInHandler;

import tigase.jaxmpp.core.client.SessionObject;

import tigase.jaxmpp.core.client.exceptions.JaxmppException;

import tigase.jaxmpp.j2se.ConnectionConfiguration.ConnectionType;

import tigase.jaxmpp.j2se.Jaxmpp;

public class Test4 {

public static void main(String[] args) {


    final Jaxmpp jaxmpp = new Jaxmpp();


    jaxmpp.getConnectionConfiguration().setConnectionType(ConnectionType.bosh);

    jaxmpp.getConnectionConfiguration().setBoshService("http://192.168.18.45:5280");

    jaxmpp.getConnectionConfiguration().setDomain("hsl.com");

    jaxmpp.getConnectionConfiguration().setPort(5280);

    jaxmpp.getConnectionConfiguration().setUserJID("ljf@hsl.com");

    jaxmpp.getConnectionConfiguration().setUserPassword("ljf");



    jaxmpp.getEventBus().addHandler(LoggedInHandler.LoggedInEvent.class, new LoggedInHandler() {


        @Override

        public void onLoggedIn(SessionObject sessionObject) {

            System.out.println("SID:" + sessionObject.getProperty("BOSH#SID_KEY"));

            System.out.println("RID:" + sessionObject.getProperty("BOSH#RID_KEY"));

        }


    });


    try {

        System.out.println("Loging in...");

        jaxmpp.login(true);


    } catch (final JaxmppException e) {

      e.printStackTrace();

    }


}

}

i got sid and rid by this way and i use it in the function connection.attach(jid,sid,rid,callback),but it does not work all the time, and i can't connect the tigase service, is my methord wrong,or there is an other way to do like this , i can Moving authentication to server-side and Maintaining session between page changes, thanks again!

    (1-3/3)