powered by Jive Software

Code to design chat client using smack

Hi i want to develop a chat component using Smack api.But i dont no How to start ,can u send any code

And i need the code to handle packet listeners

i’d try a look at this: http://www.igniterealtime.org/builds/smack/docs/latest/documentation/

(point “getting started”)

Hi

thanks for ur reply.But i alreay went through that link.I wrote some code also.But i have some doubts.

1.How to get updated presence of my roster elemnts.I knew that for that one we need to subscribe to get the updated presence.But How i can do it?

2.How i can send a message to a person who is in my roster

could u plz reply

here i am sending the bvasic code plz go through it

/////////////// login method

public boolean login(String login_name,String login_password)

{

this.login_name = login_name;

name=login_name;

loginName=new JMenu(“Hi ,” +name);

menuBar.add(loginName);

this.login_password = login_password; // get passwordfield’s password

boolean flag = false;

try{con.DEBUG_ENABLED = true;

con = new ServerConnection(“10.241.34.80”,“5222”); //Default. Will be changed later

con.connect();

con.login(login_name, login_password); /* filter the XML packet into a PacketCollector (much like a queue),

so that you can get them back later*/

presence.setType(Presence.Type.subscribed);

filter = new AndFilter(new PacketTypeFilter(Message.class),new FromContainsFilter(rentry));

myCollector = con.createPacketCollector(filter);

groupchat = con.createGroupChat(login_name+"'s Chat"); // initialise your chatgroup

System.out.println(“login name’s chat is *********”+login_name);

msg = new Message();

roster = con.getRoster(); // get all user from your friends list

roster.addRosterListener(this); //listner added to roster

setRoster();

setChat(true);

flag = true;

System.out.println(“Inside login”);

myListener=new MyListener();

con.addPacketListener(myListener, filter);

ChatClientApp chatapp=new ChatClientApp();

try

{

t=new Thread(chatapp);

t.start();

}

catch (Exception ex)

{

System.out.println("exception in thread is "+ex);

}

}

//////////////////////////////////// after clicking send button

else if(e.getSource()==btnSend)

{

if(isLogin)

{

System.out.println(“inside btnSend”);

selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

if(selectedNode==null || (selectedNode.isLeaf()==false)){

JOptionPane.showMessageDialog(frm,“Select a contact!”,“Select a Contact”,JOptionPane.INFORMATION_MESSAGE);

}

else

{

String sendCnt = selectedNode.getUserObject().toString();

String opponent =sendCnt.substring(0, sendCnt.indexOf("("));

System.out.println(“Get opponent name”+opponent);

for (Iterator i=roster.getEntries().iterator(); i.hasNext(); )

{

RosterEntry re = (RosterEntry)i.next();

rentry = re.getUser().toString();

System.out.println("after break " + rentry);

String rname = re.getName().toString();

if(rname.equalsIgnoreCase(opponent)==true)

{

System.out.println("*******Equal Contacts: " + rentry);

System.out.println(“before break”);

break;

}

}

String content = null;

try

{

content = null;

content = tfChatMessage.getText().toString();

System.out.println("Content: " + content);

con.createChat(rentry);

msg.setTo(rentry);

msg.setBody(null);

System.out.println("Message body before set, after remove: " + msg.getBody());

msg.setBody(tfChatMessage.getText().toString());

System.out.println("Message body after set: " + msg.getBody());

msg.setType(Message.Type.chat);

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

//groupchat.sendMessage(msg);

//con.sendpacket(msg);

Date current=new Date();

taChatArea.append( login_name*": “"\n"”: “*msg.getBody()+”\n");

tfChatMessage.setText(null);

chatmanager = con.getChatManager();

newChat = chatmanager.createChat(rentry, new MessageListener() {

public void processMessage(Chat chat, Message message) {

System.out.println("Received message: " + message.toXML());

}

});

try

{

System.out.println(“before sending message”);

newChat.sendMessage(msg.getBody());

System.out.println(msg.getType()+“is type of the message”);

System.out.println("message body is "+msg.getBody());

}

catch (XMPPException ex)

{

System.out.println(“Error Delivering block”+ex);

}

}

////////////////////////////// setroster method

public void setRoster()

{

roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);

roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);

System.out.println("the subscription is "+roster.getSubscriptionMode());

System.out.println(“Inside setRoster Method”);

String str=(presencevalues[http://combostatus.getSelectedIndex()|http://combostatus.getSelectedIndex()]).to String();

System.out.println(" "str"*****");

DefaultMutableTreeNode groupNode = null;

DefaultMutableTreeNode entryNode = null;

System.out.println("Top children(before): " + top.getChildCount());

if(top.getChildCount()!=0){

top.removeAllChildren();

}

System.out.println("Top children(after): " + top.getChildCount());

//Refresh the Roster

for (Iterator i=roster.getGroups().iterator(); i.hasNext(); )

{

RosterGroup rg = (RosterGroup)i.next();

groupNode = new DefaultMutableTreeNode(rg.getName());

top.add(groupNode);

System.out.println(“group added”);

for(Iterator iEntry=rg.getEntries().iterator(); iEntry.hasNext(); )

{

RosterEntry re = (RosterEntry)iEntry.next();

System.out.println("User Name is"re.getUser()"Jid name is "+re.getName());

if(re.getName().equalsIgnoreCase(“tmp”)==false)

{

if(status==null){

status = “offline”;

}

System.out.println("setRoster: Status is may be "+str);

entryNode = new DefaultMutableTreeNode(re.getName()*"("*str+")");

groupNode.add(entryNode);

System.out.println(“entry added”);

}

}

}

//////////////procass packet method

public void processPacket(Packet packet)

{

Message message = (Message)packet;

// System.out.println(message.getBody()+"…is formatted msg");

//System.out.println(message.getFrom()+">>>>>>>>is message from");

System.out.println(message.getTo()+"…message to");

chatObj=new ChatClientApp();

System.out.println("presence is "chatObj.presence"packet is "+packet.toXML());

System.out.println(“inside the process packet”);

if (packet instanceof Message)

{

System.out.println(“Inside instance method”);

System.out.println(message.getBody()+"…is formatted msg");

System.out.println(message.getFrom()+">>>>>>>>is message from");

System.out.println(message.getTo()+"…message to");

}

}

}