powered by Jive Software

Parrot-bot example

Hi!

I search why my parrot-bot example doesn’‘t work and i don’'t understand … Can anyone to help me? I use psi or JBother to connect with user2 id. I receive the first message from user1 and I reply but user1 never receive any message…

Thanks for your help!

import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.packet.Message;

/**

  • @author ****

  • @version 15 mars 2004

*/

public class Test {

private static String serveur = “10.194.66.120”;

private static String user1 = “user1”;

private static String user2 = “user2”;

public static void main(String[] args) {

try {

XMPPConnection connection = new XMPPConnection(serveur);

connection.login(user1, user1);

Chat chat = connection.createChat(user2 + “@” + serveur);

chat.sendMessage(“First message.”);

while (true) {

Message reply = chat.nextMessage(1000);

if (reply != null) {

System.out.println(">Message : " + reply.getBody() + " From : " + reply.getFrom() + " To : " + reply.getTo());

chat.sendMessage("Reply : " + reply.getBody());

} else {

System.out.println(“No Message.”);

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

What is the problem you’'re having?

Regards,

Matt

Well, my parrot-bot never received any messages which I sent with my Jabber client (such as JBother or Psi)…

I tried your code and it worked fine for me with Psi and another closed source client. Are both “user1” and “user2” registered on your server?

Here’'s some code of another parrot bot that only needs to know about one user when starting up:

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet; public class ParrotBot implements PacketListener {
   private XMPPConnection connection = null;
   private Chat chat = null;
   private PacketFilter filter = null;
   private static String server = "jabber.server.ip";
   private static String user = "test.user";    public static void main(String[] args) {
      new ParrotBot().squawk();
   }    public ParrotBot() {
      try {
         XMPPConnection.DEBUG_ENABLED = true;
         connection = new XMPPConnection(server);
         connection.login(user, "test");
         filter = new PacketTypeFilter(Message.class);
         connection.addPacketListener(this, filter);
      } catch (XMPPException e) {
         e.printStackTrace();
      }
   }    private void squawk() {
      while (true) {
         try {
            Thread.sleep(1000);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }    public void processPacket(Packet packet) {
      Message message = (Message) packet;
      chat = connection.createChat(packet.getFrom());
      try {
         chat.sendMessage(message.getBody());
      } catch (XMPPException e) {
         e.printStackTrace();
      }
   }
}

Wonderfull!!! It’'s work fine!

Thank you!

But I still not understand why my code doesn’'t work for me!

Thank you!

Try changing this line:

Chat chat = connection.createChat(user2 + "@" + serveur);

to this:

Chat chat = connection.createChat(user2);

No it isn’'t the problem because the value of user1 is “user1” but not "user1@serveur.com"

I still not understand why the nextMessage’‘s method doesn’'t work but the processPacket works fine…

I forget something, I don’'t?

Thanks for your help!

Amazing!!!

It’‘s doesn’'t work with RH9 and JDK1.4.2_03-b02!!!

Amazing!!!

It’‘s doesn’'t work with RH9 and JDK1.4.2_03-b02!!!

That is strange. Not that it should make any differen but were/are you running the client and server on the same machine?

There maybe a bug in either the Smack API or in Java since there has been at least one other person with a similar problem:

That’‘s interesting. I just tried the standard parrot-bot example (from the Smack docs), and it doesn’'t work on Mac OS X 10.2 (1.4.1_01) or RH9 (1.4.2_04). Same symptoms – it creates the Chat and sends the initial message, but then never receives any reply sent by my other Jabber client (Nitro).

However, another program that just dumps all packets received to System.out, using a PacketListener, works just fine.

Mark,

However, another program that just dumps all packets

received to System.out, using a PacketListener, works

just fine.

Are you adding the PacketListener to the XMPPConnection or to the Chat? If you are adding the PacketListener to the XMPPConnection I’'d then like to know the filter that you are using.

By default, the Chat will filter messages whose threadID matches the Chat’‘s threadID. Since the threadID is optional in XMPP, it’'s possible that this is generating the problem. You can open a debugger and check if the received packet includes a thread attribute or not. If you are not receiving it then you can set the Chat to NOT filter based on threadID. This will cause to filter the packets on ((Message.Type.CHAT and participant) or threadID).

Regards,

– Gato