Wednesday, September 30, 2015

asmack Android XMPP Login and Multi User Chat


ConnectionConfiguration config = new ConnectionConfiguration(SERVER_IPADDRESS, SERVER_PORT);
config.setDebuggerEnabled(true);
XMPPConnection connection = new XMPPConnection(config);
SmackConfiguration.setPacketReplyTimeout(100000);
connection.connect();

Now you are connected to server.

ChatManager chatManager = connection.getChatManager();

If you want to create group you have to login to the server using XMPP connection object like below.
connection.login("userName", "Password");

You can also login anonymously like this if your server allows you.
connection.LoginAnonymously();

Log.v("LoggedIn : ", "hammad");


// connection.getRoster().createGroup("hammamd");

If you donot specify correct Domain Name which your server recognize you will get Exception.
like Remote Server Not found or Not Authorised or Service not available.


MultiUserChat multiUserChat = new MultiUserChat(connection,"admin" + "@" + DOMAIN_NAME);

Log.v("Group join : ", "hammad");

This statment will join group with nick name hammad. if no group exist it will create a new group.
multiUserChat.join("hammad");


Than you can add message listner for reading/parsing incoming messages. like below. here you should extract message from packet object by converting it into message type class. 
PacketFilter filter = new MessageTypeFilter(org.jivesoftware.smack.packet.Message.Type.groupchat);connection.addPacketListener(new PacketListener() {
    @Override    public void processPacket(Packet packet) {
        org.jivesoftware.smack.packet.Message message = (org.jivesoftware.smack.packet.Message) packet;        if (message.getBody() != null) {
            String from = message.getFrom();            String Body = message.getBody();            // Add incoming message to the list view or similar        }
    }
}, filter);

with this line a message will be sent to group..

multiUserChat.sendMessage("hi group, I am Hammad");

If you want to get all registered users from server use below code.
You will need this code when you want to sync phone contacts with server, after getting all registered users from server in a list, read your android phone contacts and compare for contacts syncing.
If you want to search for a specific user from server replace * in the code with user name which you want to search.
do not forget to send configurations. use this this link for refrence, how to add configurations.
get Registered users from openfire server.


UserSearchManager search = new UserSearchManager(connection);try {
    Form searchForm = search.getSearchForm("search." + connection.getServiceName());    Form answerForm = searchForm.createAnswerForm();    answerForm.setAnswer("Username", true);    answerForm.setAnswer("search", "*");    System.out.println("search form");    ReportedData data = search.getSearchResults(answerForm, "search." + connection.getHost();    if (data.getRows() != null) {
        Iterator<ReportedData.Row> it = data.getRows();        while (it.hasNext()) {
            
            ReportedData.Row row = it.next();            Iterator iterator = row.getValues("jid");            if (iterator.hasNext()) {
                String value = iterator.next().toString();                user_list.add(value);
                // use variable user_list. List<String> user_list. initialize this variable.

             }
        }
    } else {
             // No contacts Found
    }
} catch (XMPPException e1) {
    // TODO Auto-generated catch block    e1.printStackTrace();}




There is another method of creating and joinning multi user group chat which is available at
below link.

http://stackoverflow.com/questions/32822357/create-persistent-group-in-xmpp-clientusingasmacklibrary-in-android





No comments:

Post a Comment

Kotlin Android MVP Dagger 2 Retrofit Tutorial

http://developine.com/building-android-mvp-app-in-kotlin-using-dagger-retrofit/