|
package jj.chen;
/*
* 本程序用到了www.jivesoftware.org的smack api
* 如果大家对googletalk有兴趣可以去以上网站自己下一个api
* 祝愿googletalk一路走好!
*/
import java.io.Serializable;
import java.util.Collection;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.GoogleTalkConnection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
public class Test implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5102956900764101510L;
/**
* @param args
* @throws XMPPException
* @throws XMPPException
*/
public static void main(String[] args) throws XMPPException {
// TODO Auto-generated method stub
final GoogleTalkConnection con = new GoogleTalkConnection();
con.login("jjchen831", "123456");//googletalk的 用户名, 密码
System.out.println("===========1");
Presence presence = new Presence(Presence.Type.AVAILABLE);
con.sendPacket(presence);
System.out.println("===============2");
final Roster roster = con.getRoster();
roster.addRosterListener(new RosterListener() {
public void presenceChanged(String user) {
// If the presence is unavailable then "null" will be printed,
// which is fine for this example.
System.out.println("resence changed: "
+ roster.getPresence(user));
if(roster.getPresence(user)!=null){
final Chat chat=con.createChat(user);
try {
chat.sendMessage("hello");//给每个在线人说hello
chat.addMessageListener(new PacketListener(){
public void processPacket(Packet arg0) {
// TODO Auto-generated method stub
System.out.println("acketListener");
Message message=chat.nextMessage(); //得到别人给你的信息
try {
f(message.getBody().equals("help")){
//如果别人给你的信息是help
chat.sendMessage("thank you for test robot!");
//就发给他thank you for test robot!
}else{
chat.sendMessage(" please enter help ");
//如果别人发给你的信息不是help就发给他 please enter help
}
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
system.out.println(user+"++++++++");
}
}
public void entriesAdded(Collection arg0) {
// TODO Auto-generated method stub
System.out.println("entriesAdded");
}
public void entriesUpdated(Collection arg0) {
// TODO Auto-generated method stub
System.out.println("entriesUpdated");
}
public void entriesDeleted(Collection arg0) {
// TODO Auto-generated method stub
System.out.println("entriesDeleted");
}
});
System.out.println("===============3");
while(true){
try {
Thread.sleep(500000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} |
|