PacketListener wont "hear" message from Smack but will "hear" Spark message

Hello all,

I found a simple example of a PacketListener class (I believe it was from Jon Wright) Thanks!

public class MessageListener implements PacketListener {

public void processPacket(Packet packet) {

Message message = (Message)packet;

Message.Type messageType = message.getType();

System.out.println(“RUNNING Message LIstener”);

if(messageType == Message.Type.CHAT) {

System.out.println("Message From: " + message.getFrom());

System.out.println("Message Body: " + message.getBody());

}

else

{

System.out.println(“NOT A CHAT”);

}

}

}

/code

If I run this using his Connect main class, it will only “hear” messages from Spark clients and not from my initial attempt at a web based client utilizing Smack libs. My client is successful at sending messages to a Spark client, so I know that part works. Any ideas?

Here is some pertinent code from my app.

public void chat(String msg,String user) throws XMPPException

{

Chat chat = conn.createChat(user+"@asterisk1.local/Spark");

Message message = chat.createMessage();

message.setBody(msg);

chat.sendMessage(message);

}

//here is the connection method

public void getConnection(String user, String pwd) throws XMPPException

{

if ( ( conn != null ) && ( conn.isConnected() ) )

{

System.out.println(“ALREADY CONNECTED!”);

}

else

{

conn = new XMPPConnection(HOST,PORT);

conn.DEBUG_ENABLED = true;

//The below creation of a packet listener will not work

//PacketTypeFilter filter = new PacketTypeFilter(Message.class);

//conn.addPacketListener(new MessageListener(), filter);

conn.login(user,pwd);

System.out.println(user+" IS CONNECTED");

}

}

/code

Basically I have two questions.

  1. why wont this listener “hear” a my Smack implemented chat.

  2. How can I create a Listener without a main.(is this even possible?)

TIA!!

Hi,

you already set “DEBUG_ENABLED = true;” so I wonder if you are receiving the messages but they do not get displayed, if create-connection did fail (a 404 error somewhere in the debug window log).

Within Spark F12 enables the debug window, so you should be able to compare what’'s going wrong.

LG

As LG said, check the debugger window to check whether or not you are receiving the Message packets.

I’‘ve tested the simple PacketListener using a variety of clients and all proved successful. I havent tested it with a web based client I’'m afraid though, so there may be some subtle differences involved.

Jon

I was testing this with 4 users connected:

  1. test_user1@asterisk1.local/Spark

  2. test_user1@asterisk1.local/Smack

  3. test_user2@asterisk1.local/Spark

  4. test_user2@asterisk1.local/Smack

This list came from the WiFi admin console client sessions page.

I closed Spark on both machines so that I had 2 users:

  1. test_user1@asterisk1.local/Smack

  2. test_user2@asterisk1.local/Smack

Now it works.

My problem is now:

If user_1logs in and sends a message. The listener returns:

Message From: user1@asterisk1.local/Smack

Messge Body: test message from user1

If user2 logs in and sends a message, I get the following:

Message From: user2@asterisk1.local/Smack

Message Body: test message from user2

All is fine.

Then if I go back to user 1 and send a message I get the following:

Message From: user2@asterisk1.local/Smack

Message Body: test message from user1.

If I then refresh the connection from user1 to the server and send a message all is fine.

Basically what is happening is that the Message From: will return the value of the last user that created a connection.

I am not sure what is happening here.

Do I make a connection to the server for each user?

Do I create a Listener with a Filter for each user?

Thanks for any advice.

Hi,

so you have the problem with resources.

As far as I know each resource has a priority one can set (probably not within Spark, I did not see the option) and if “/Smack” has got the highest priority it seems that the server does set the “from” field for you to the resource with the highest priority. So it seems the connection which was made last always gets the highest priority, that’'s what one may not expect.

See http://www.jabber.org/jeps/jep-0168.html#primary for details, unfortunately I have no idea about the Smack method to do this but I assume that you want to set it, but the API /smack/packet/Presence.html should help you to find it.

edited: it’'s #setPriority(int priority)

LG

Thanks LG ,

I will have a look at that.

Could you tell me if I am correct in assuming the following:

  1. For each user (client) I would create its own connection object.

conn = new XMPPConnection(HOST,PORT);

/code

  1. For each user (client) I would create its own filter.

PacketTypeFilter filter = new PacketTypeFilter(Message.class);

/code

  1. For each user (client) I would create its own packet listener

conn.addPacketListener(new MessageListener(), filter);

/code

Does this seem like the proper logic?

TIA!!

imo you would need to create a new XMPPConnection for each client that wanted to connect. In your Connection class you would add a packetfilter and packetlistener to the connection object.

So from what I can see your thinking looks good. Mind you I havent created a Instant Messaging web app so I could be wrong

Revisiting this problem…

//MY CONNECTION METHOD

public void getConnection(String user, String pwd) throws XMPPException

{

conn = new XMPPConnection(HOST,PORT);

conn.DEBUG_ENABLED = true;

PacketTypeFilter filter = new PacketTypeFilter(Message.class);

conn.addPacketListener(messageListener, filter);

conn.login(user,pwd);

}

//LISTENER METHOD

PacketListener messageListener = new PacketListener()

{

public void processPacket(Packet packet)

{

Message message = (Message)packet;

String from = message.getFrom();

String body = message.getBody();

String to = message.getTo();

System.out.println(from*" SAYS “body” to "*to);

}

};

//CREATE CHAT METHOD

public void chat(String msg,String user) throws XMPPException

{

Chat chat = conn.createChat(user+"@asterisk1.local");

Message message = chat.createMessage();

message.setBody(msg);

chat.sendMessage(message);

}

/CODE

Jim logs in.

connection, listener, filter created for Jim.

Mary logs in

connection, listener, filter created for Mary.

They begin to chat (This is the result of the System.out from the MessageListener method above:

Jim IS CONNECTED

Mary IS CONNECTED

Jim sends the first message:

Mary@asterisk1.local/Smack SAYS Hello Mary to Mary@asterisk1.local

Mary sends the next:

Mary@asterisk1.local/Smack SAYS hello Jim to Jim@asterisk1.local

Jim sends the next:

Mary@asterisk1.local/Smack SAYS Hello again Mary to Mary@asterisk1.local

Mary sends the last:

Mary@asterisk1.local/Smack SAYS Hello again Jim to Jim@asterisk1.local

The to object is correct, the from object is always showing as Mary (last one to connect)

Looks like the last listener that is registered overrides or kills? all other listeners.

The connections still stay alive (looking at the sessions tab in wifi admin portal tells me this ) the listeners dont seem to. Any ideas why this would happen?

Is there a way to uniquely identify a listener object?

Thanks again.