Newbie question

I’‘m new to Smack and XMPP and Jive. I installed Jive and set up two users. I have a simple Java client using Smack and can launch two clients on one machne – the same machine as the Jive server. I can connect and login. If I start a chat so that userA is talking with userA, then userA gets their own messages sent back to them fine. … But I can’'t get userA to talk to userB.

I’'ve tried getting the roster but it shows no entries even when Jive shows both users as online… Do I have to set up a group or something to get users to see each other?

Thanks,

boz

Message was edited by:

bozmoz

hi

  1. use the xmppconnection to getRoster()

  2. set the subscription mode (say Roster.SUBSCRIPTION_ACCEPT_ALL)

  3. add a roster listener

  4. Then add the users to each others roster.

  5. Start chating.

I hope this helps

Hello rajdas,

Thanks very much for the reply. I think that’‘s what I forgot to do. I’‘ve tried to do it but still have not succeeded. I think I still don’'t know how to do your #4 “Then add the users to each others roster.”

Below is the code I wrote. Before it is called, a private final XMPPConnection fConnection is initialized and a user is logged in. The private final fields fUserAddress and fUserNickName have been initialized too.

private void initializeRoster()

{

Roster roster = fConnection.getRoster();

roster.setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);

roster.addRosterListener(new RosterListener()

{

public void rosterModified()

{

LOG.debug("------ Roster Modified -


");

}

public void presenceChanged(String user)

{

LOG.debug("------ Presence Changed -


");

try

{

Roster roster = fConnection.getRoster();

roster.createEntry(user, user, null);

}

catch (XMPPException e)

{

e.printStackTrace();

}

}

});

try

{

roster.createEntry(fUserAddress, fUserNickName, null);

}

catch (XMPPException e)

{

e.printStackTrace();

}

}

So my test runs and I can see that two users log in, but neither sees the other in the roster. They only see themselves, and the messages don’‘t show up. Heres some of the log, if it’'s of any help…

joe:: Logged in with name=’‘joe’’, password=’‘joe’’

joe:: Got Smack Chat to jim@jenningschr1

Created roster entry for self: joe

Roster set up for org.jivesoftware.smack.Chat@2ce908


Roster Modified -


jim:: Logged in with name=’‘jim’’, password=’‘jim’’

jim:: Got Smack Chat to joe@jenningschr1

Created roster entry for self: jim

Roster set up for org.jivesoftware.smack.Chat@1f03691

joe:: sent message: org.jivesoftware.smack.packet.Message@52c6b4

jim:: sent message: org.jivesoftware.smack.packet.Message@738d08


Roster Modified -


Thanks again!

,boz

Turns out the code was right but I was running it at the wrong time. I was trying to set up the chats as I created them. I’'ve changed it so there is a phase where the clients login, and then the chat is created only once both have had a chance to be registered.

Thanks for the help! I would have fiddled around with the wrong assumptions much longer without it!

,boz

I thought I got it, but I’‘m having no luck again. I’‘m not sure what I’‘m doing wrong. Here’‘s more code. It doesn’‘t have roster listeners but putting them in seems to make no difference. Below the code is sample output. … PLEASE HELP!! :] I’'ve been trying for hours.

TIA, Chris

— start code —

public void test2SmackChats()

{

try

{

class MyMessageListener implements PacketListener

{

boolean gotMessage = false;

public void processPacket(Packet packet)

{

System.out.println("got message: " + packet); // never happens

MyMessageListener.this.gotMessage = true;

}

public String toString() {return “gotMessage=” + gotMessage;}

}

XMPPConnection joeConnection = new XMPPConnection(“jenningschr1”, 5222);

XMPPConnection jimConnection = new XMPPConnection(“jenningschr1”, 5222);

joeConnection.login(“joe”, “joe”);

jimConnection.login(“jim”, “jim”);

joeConnection.getRoster().setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);

jimConnection.getRoster().setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);

PacketListener joeMessageListener = new MyMessageListener();

PacketListener jimMessageListener = new MyMessageListener();

Chat joeChat = joeConnection.createChat(“jim@jenningschr1”);

Chat jimChat = jimConnection.createChat(“joe@jenningschr1”);

joeChat.addMessageListener(joeMessageListener);

jimChat.addMessageListener(jimMessageListener);

joeChat.sendMessage(“hi jim”);

jimChat.sendMessage(“hi joe”);

pause(5000); // calls a mothod that pauses that many milliseconds

System.out.println(“After 5 seconds…”);

System.out.println(" joeMessageListener: " + joeMessageListener);

System.out.println(" jimMessageListener: " + jimMessageListener);

System.out.println(“Joe’'s roster entries…”);

Iterator joeRosterEntriesItr = joeConnection.getRoster().getEntries();

while (joeRosterEntriesItr.hasNext())

{

System.out.println(" " + joeRosterEntriesItr.next());

}

System.out.println(“Jim’'s roster entries…”);

Iterator jimRosterEntriesItr = jimConnection.getRoster().getEntries();

while (jimRosterEntriesItr.hasNext())

{

System.out.println(" " + jimRosterEntriesItr.next());

}

}

catch (XMPPException e)

{

e.printStackTrace();

}

}


end code —

— start output —

After 5 seconds…

joeMessageListener: gotMessage=false

jimMessageListener: gotMessage=false

Joe’'s roster entries…

jim@jenningschr1: jim@jenningschr1

joe: joe@jenningschr1

Jim’'s roster entries…

jim: jim@jenningschr1

joe@jenningschr1: joe@jenningschr1

— end output —

Thought I got it, but no. see the last post.

I got it to work by calling…

Chat.setFilteredOnThreadID(false); // must be set before creating chats

… before creating the chats.

Now I have a new question: “How do you get two chat’'s on the same threadID?” but I’'ll look in the archives and then maybe post a new thread here.

,Chris