Recieve Messages

Hi

I have the following snippet of code. Currently I’'m able to send messages from the client, but when I the 2nd client wants to send a message back it does not appear in my TextArea. Can someone please look at the code at see why it is not recieving incoming packets???

Thanks

Dilip

public void actionPerformed (ActionEvent evt)

{

try

{

XMPPConnection connection = new XMPPConnection (“localhost”);

connection.login (“pateldw”, “intel1”);

Chat newChat = connection.createChat (“test@localhost”);

String text = textField.getText ();

textArea.append (text + newline);

newChat.sendMessage (text + newline);

textField.selectAll ();

PacketFilter filter = new PacketTypeFilter (Message.class);

TextDemo listener = new TextDemo ();

connection.addPacketListener (listener, filter);

}

catch (Throwable t)

{

System.out.println (“Got Error in costructor”);

t.printStackTrace (System.out);

}

}

public void processPacket (Packet packet)

{

Message message = (Message) packet;

textArea.append (packet.getFrom ());

textArea.append (message.getBody ());

textField.selectAll ();

}

Are you subscribed with the other user? A simple thing to do beforehand is to just get subscribed by sending the subscribe packet.

ex.

Presence p = new Presence( Presence.Type.Subscribe );

p.setTo( whoever );

con.sendPacket( p );

Also, what is TextDemo? If this is the listener, then where is the code to capture the message packet?

Hi,

Here is the full code. It is just one class. I have already subcribed to the user who im sending to through another client. If I replace the textArea.append line with System.out.println then the incoming packet is displayed in the console window. But using textArea.append does not show the incoming packet. Please look at the code below.

Thanks

Dilip

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.util.*;

import java.text.*;

import org.jivesoftware.smack.*;

import org.jivesoftware.smack.packet.Packet;

import org.jivesoftware.smack.packet.Presence;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.filter.*;

public class TextDemo extends JPanel implements ActionListener, PacketListener

{

public static void main (String[] args) throws Exception

{

XMPPConnection connection = new XMPPConnection (“localhost”);

connection.login (“pateldw”, “intel1”);

javax.swing.SwingUtilities.invokeLater (new Runnable ()

{

public void run ()

{

createAndShowGUI ();

}

}

);

}

JTextField textField;

JTextArea textArea;

JEditorPane editorPane;

private final static String newline = “\n”;

public TextDemo ()

{

super (new GridBagLayout ());

textField = new JTextField (20);

textField.addActionListener (this);

textArea = new JTextArea (5, 20);

textArea.setEditable (true);

JScrollPane scrollPane = new JScrollPane (textArea,

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Add Components to this panel.

GridBagConstraints c = new GridBagConstraints ();

c.gridwidth = GridBagConstraints.REMAINDER;

c.fill = GridBagConstraints.HORIZONTAL;

add (textField, c);

c.fill = GridBagConstraints.BOTH;

c.weightx = 1.0;

c.weighty = 1.0;

add (scrollPane, c);

}

public void actionPerformed (ActionEvent evt)

{

try

{

XMPPConnection connection = new XMPPConnection (“localhost”);

connection.login (“pateldw”, “intel1”);

Chat newChat = connection.createChat (“test@localhost”);

String text = textField.getText ();

textArea.append(text + newline);

newChat.sendMessage (text + newline);

textField.selectAll ();

PacketFilter filter = new PacketTypeFilter (Message.class);

TextDemo listener = new TextDemo ();

// listener.start ();

connection.addPacketListener (listener, filter);

}

catch (Throwable t)

{

System.out.println (“Got Error in costructor”);

t.printStackTrace (System.out);

}

//Make sure the new text is visible, even if there

//was a selection in the text area.

textArea.setCaretPosition (textArea.getDocument ().getLength ());

}

public void processPacket (Packet packet)

{

Message message = (Message) packet;

textArea.append(packet.getFrom ());

textArea.append(message.getBody ()):

textField.selectAll ();

}

/**

  • Create the GUI and show it. For thread safety,

  • this method should be invoked from the

  • event-dispatching thread.

*/

private static void createAndShowGUI ()

{

//Make sure we have nice window decorations.

JFrame.setDefaultLookAndFeelDecorated (true);

//Create and set up the window.

JFrame frame = new JFrame (“TextDemo”);

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

JComponent newContentPane = new TextDemo ();

newContentPane.setOpaque (true); //content panes must be opaque

frame.setContentPane (newContentPane);

//Display the window.

frame.pack ();

frame.setVisible (true);

}

}

From eyeballing your code, I would say remove the xtra instance of textdemo which should not be there and just specify “this” for your listener. This way you will actually be appending to the correct textarea.

ex.

PacketFilter filter = new PacketTypeFilter (Message.class);

// listener.start ();

connection.addPacketListener (this, filter);

Thanks…It WORKS!!! JIVE ROCKS!

Having one last problem…

How would I call newChat in the actionPerformed method? Currently I have two instances of XMPPConnection, having XMPPConnection in actionPerformed is causing the client to disconnect and reconnect everytime I send a message, which causes the server to crash. Any Ideas?

Thanks Dilip

Just specify XMPPConnection as a class variable and then initialize it only once in your constructor.

ex.

XMPPConnection con = null;

public Foo() {

con = new XMPPConnection( “localhost” );

}

public void actionPerformed( ActionEvent ) {

Chat newChat = con.createChat( “dude@localhost” );

newChat.sendMessage( “Hello dude” );

}

Hi

When I use the code above to initialise the connection I get a NULLPointerException when I send a Message. Any Ideas?

Thanks Dilip

Hi

I managed to solve the connection problem.

The next thing I want to do is get the nick name of the JID as a recieved message.

Example:

Sent Message:

:Hello World

Recieved Message

:Hi There

As you can see the nickname of that person should appear. I read the docs I think not sure I need to use getParticipant or getChatID. Can someone show me how I would use this to display the nick name in my client?

Thanks

Dilip