powered by Jive Software

Presence is incorrect and can't send message

I have a problem getting the presence of the other user with code. There is nothing wrong with the users configuration and the openfire server as i’ve tested everything with the SPARK client application.

on the line below

System.out.println(entry + " is " + roster.getPresence(“userB”));

I always get unavailable despite userB being online. Also I cannot send a chat message.

I’ve also tried adding a Thread sleep(5000) after connecting as suggested in another forum but to no avail. Any ideas why this isn’t working?

I’m using the smack API and my code below is as follows

package com.order.xmpp; import java.io.IOException;
import java.util.Calendar;
import java.util.Collection; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence; public class SendTestOpenFire {     public static void main(String[] args) {         System.out.println("Starting IM client");         // gtalk requires this or your messages bounce back as errors
        ConnectionConfiguration connConfig = new ConnectionConfiguration("MY_SERVER", 5222);
        XMPPConnection connection = new XMPPConnection(connConfig);         try {
            System.out.println("Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            System.out.println("Failed to connect to " + connection.getHost());
        try {
            connection.login("userA", "userA");             System.out.println("Logged in as " + connection.getUser());             Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);             //getRoster
            Roster roster = connection.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            for (RosterEntry entry : entries) {
                System.out.println(entry + " is " + roster.getPresence("userB"));
            }         } catch (XMPPException ex) {
            System.out.println("Failed to log in as " + connection.getUser());
        }         ChatManager chatmanager = connection.getChatManager();         /*//Add chat  listener
        chatmanager.addChatListener(new ChatManagerListener() {
            public void chatCreated(Chat chat, boolean b) {
                chat.addMessageListener(new MessageListener() {
                    public void processMessage(Chat chat, Message message) {
                        System.out.println("Received chat message " + (message != null ? message.getBody() : "NULL"));
        });*/         Chat chat = chatmanager.createChat("userB", new MessageListener() {
            public void processMessage(Chat chat, Message message) {
                System.out.println("Received Message: " + message.getBody() + " | " + message.getSubject());
        });         try {
            // google bounces back the default message types, you must use chat
            System.out.println("SENDING MESSAGE!!!!");
            Message msg = new Message("userB", Message.Type.normal);
            msg.setSubject("TEST SUBJECT");
            msg.setBody("SOME BODY");
        } catch (XMPPException e) {
            System.out.println("Failed to send message");
            // handle this how?
        }         System.out.println("Press enter to disconnect");         try {
        } catch (IOException ex) {
        }         connection.disconnect();
    } }

So everyone here has had 100% success with code like this?

I am connecting to Facebook and it is showing me wrong presence…