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.
-
why wont this listener “hear” a my Smack implemented chat.
-
How can I create a Listener without a main.(is this even possible?)
TIA!!