Packet Listeners

Hi again,

I’'ve been having a bit of trouble when displaying the text of a recieved message,

It displays the message ok the first or second lines but after that it seems to display the lines an exponential number of times,

Do you know if this is a common thing, I think maybe the packet listener is fired twice but i’'m new to this so not 100% certain.

Thanks

Bob

Also Does Anyone know the procedure for an incoming message to alert the user of the message,i.e. trigger a dialog or something to say a msg from xxx is waiting?

Thanks Again

Bob

Bob,

Can you paste in the code you’'re using?

Regards,

Matt

Also Does Anyone know the procedure for an incoming

message to alert the user of the message,i.e. trigger

a dialog or something to say a msg from xxx is

waiting?

This would entirely depend on your client application. Some client apps would want to show a Swing app while others would log the message to the console, etc. Is there a particular bit of code you’'re having troubles with?

Regards,

Matt

Ok Matt,

Heres the code, To be honest i’‘ve gotten most of it off this site already which has been brilliant, I was trying to use a different api but somebos=dy advised me of smack and it’'s much easier,

What i wanted to do was show a dialog to the user when the message is recieved.

this way the chat could be initialised ???

because i couldnt see the first line received untill the recieving client sends a msg,

(the sending client sends msgs but the recieving client doesnt start recieving them untill it sends a msg)

Sorry if it’'s hard to follow.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.PacketListener;

import org.jivesoftware.smack.packet.Packet;

import org.jivesoftware.smack.filter.PacketTypeFilter;

import org.jivesoftware.smack.filter.PacketFilter;

import org.jivesoftware.smack.Roster;

import org.jivesoftware.smack.RosterEntry;

import org.jivesoftware.smackx.packet.Time;

import org.jivesoftware.smackx.MessageEventManager;

import org.jivesoftware.smackx.DefaultMessageEventRequestListener;

import java.util.Iterator;

import org.jivesoftware.smackx.XHTMLText;

import org.jivesoftware.smackx.XHTMLManager;

public class sendim extends JFrame implements PacketListener{

private JPanel contentPane;

/////

/// Unnecessary code

/////

private String recipient , host = “localhost” , newline = “\n” , tab = “\t” , nick = “” , user ="";

time ntime = new time();

private Roster roster;

private XMPPConnection con;

public void setto(String to)

{

recipient = to;

}

public void setdetails(user newUser, XMPPConnection Con)

{

con = Con;

String Username = newUser.getUsername();

String Password = newUser.getPassword();

String Host = newUser.getHost();

int port = newUser.getPort();

newuser.setdetails(Username,Password,Host,port);

}

/*Construct the frame/

public sendim() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

/**

  • Component initialization
  • @throws Exception

*/

private void jbInit() throws Exception {

///

////unnecessary code

////

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(null);

this.setSize(new Dimension(500,420));

this.setVisible(true);

this.setResizable(false);

this.setTitle("Instant Messsage : ");

this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);

menuFile.setText(“File”);

menuFileExit.setText(“Exit”);

menuFileExit.setIcon(image5);

menuFileExit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

exitIm_actionPerformed(e);

}

});

menuFileSend.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

sendIM_actionPerformed(e);

}

});

MenuOptionsViewImConversation.setIcon(image4);

MenuOptionsViewImConversation.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MenuOptionsViewImConversation_actionPerformed(e);

}

});

menuHelpHelp.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

menuHelpHelp_actionPerformed(e);

}

});

messageField.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

messageField_actionPerformed(e);

}

});

jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

jsp.setDoubleBuffered(true);

jsp.setToolTipText(“Your Chat”);

menuFile.add(menuFileSend);

menuFile.addSeparator();

menuFile.add(menuFileExit);

menuOptions.add(MenuOptionsViewImConversation);

menuHelp.add(menuHelpHelp);

menuBar.add(menuFile);

menuBar.add(menuOptions);

menuBar.add(menuHelp);

conversation.setSize(496,300);

conversation.setEditable(false);

conversation.setEnabled(true);

conversation.setBackground(Color.white);

conversation.setLineWrap(true);

conversation.setVisible(true);

conversation.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));

jsp.setBounds(2,2,490,250);

jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

labelMessage.setText(“Message”);

labelMessage.setBounds(2,255,80,20);

labelMessage.setVisible(true);

messageField.setEditable(true);

messageField.setVisible(true);

messageField.setBounds(2,275,490,20);

messageField.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));

sendIM.setBounds(2,300,80,60);

sendIM.setVisible(true);

sendIM.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

sendIM_actionPerformed(e);

}

});

//sendIM.setIcon(image6);

sendIM.setVerticalTextPosition(SwingConstants.BOTTOM);

sendIM.setHorizontalTextPosition(SwingConstants.CENTER);

sendIM.setIcon(image1);

saveConButton.setVerticalTextPosition(SwingConstants.BOTTOM);

saveConButton.setHorizontalTextPosition(SwingConstants.CENTER);

saveConButton.setBounds(new Rectangle(85, 300, 150, 60));

saveConButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

saveConButton_actionPerformed(e);

}

});

saveConButton.setIcon(image2);

saveConButton.setText(“Save Conversation”);

exitIm.setBounds(237,300,80,60);

exitIm.setIcon(image3);

exitIm.setVerticalTextPosition(SwingConstants.BOTTOM);

exitIm.setHorizontalTextPosition(SwingConstants.CENTER);

exitIm.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

exitIm_actionPerformed(e);

}

});

contentPane.add(sendIM,null);

contentPane.add(messageField,null);

contentPane.add(jsp,null);

contentPane.add(labelMessage, null);

contentPane.add(saveConButton);

contentPane.add(exitIm);

this.setJMenuBar(menuBar);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable() {

public void run() {

try {

XMPPConnection.DEBUG_ENABLED = true;

} catch (Exception exception) {

exception.printStackTrace();

}

sendim newim = new sendim();

}

});

}

void sendIM_actionPerformed(ActionEvent e)

{

String now = ntime.getHrMin();

try

{

Iterator it = con.getRoster().getEntries();

while (it.hasNext())

{

RosterEntry entry = (RosterEntry) it.next();

if(entry.getName().equals(recipient))

{

System.out.println(entry.getName());

user = entry.getUser();

nick = entry.getName();

System.out.println(user);

}

System.out.println(entry.getName());

System.out.println(entry.getUser());

}

/***

  • Creating a chat with the Chossen Person

*/

PacketFilter filter = new PacketTypeFilter (Message.class);

con.addPacketListener (this, filter);

Chat newChat = con.createChat (user);

/*******************************************************************

  • Getting the Message and sending it to the desired recipient

******************************************************************/

String text = messageField.getText();

conversation.append("(“now”)“newuser.getUsername()” :"+text + “\n”);

Message msg = newChat.createMessage();

msg.setBody(text);

//MessageEventManager.addNotificationsRequests(msg, true, true, true, true);

newChat.sendMessage (msg);

messageField.setText ("");

}

catch (Throwable t)

{

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

t.printStackTrace (System.out);

}

System.out.println(“Message sent to :”+recipient);

//JOptionPane.showMessageDialog(null,"@" +time,“time”,JOptionPane.PLAIN_MESSAGE);

messageField.selectAll();

}

void exitIm_actionPerformed(ActionEvent e) {

int n = JOptionPane.showConfirmDialog(null,“Are You sure you want to exit?”,“Exit”,JOptionPane.YES_NO_OPTION);

if(n ==JOptionPane.YES_OPTION)

{

dispose();

}

}

public void saveConButton_actionPerformed(ActionEvent e)

{

String conv = conversation.getText();

CreateIm newim = new CreateIm();

String save = newim.totoString(conv);

newim.doCreate(save);

time newtime = new time();

JOptionPane.showMessageDialog(null,“Chat Saved @:”+newtime.gettime(),“Saved”,JOptionPane.INFORMATION_MESSAGE);

}

void MenuOptionsViewImConversation_actionPerformed(ActionEvent e)

{

ViewConversation chatview = new ViewConversation();

Dimension dlgSize = chatview.getPreferredSize();

Dimension frmSize = getSize();

Point loc = getLocation();

chatview.setLocation((frmSize.width - dlgSize.width) /4 + loc.x, (frmSize.height - dlgSize.height) / 8 + loc.y);

chatview.show();

}

public void menuHelpHelp_actionPerformed(ActionEvent e)

{

HelpFrame newhelp = new HelpFrame();

}

public void messageField_actionPerformed(ActionEvent e)

{

sendIM_actionPerformed(e);

}

public void processPacket (Packet packet)

{

Chat.setFilteredOnThreadID(false);

//Roster roster;

String now = ntime.getHrMin();

Message message = (Message) packet;

String RecievedMsg = message.getBody();

conversation.setText(conversation.getText() + “(” + now + “)<” + nick +

“>:” + message.getBody() + newline);

//conversation.append("(“now”)<"+ nick+ ">:"message.getBody () newline);

messageField.selectAll();

}

}