1 import java.util.*;
  2 import java.io.*;
  3 
  4 import org.jivesoftware.smack.Chat;
  5 import org.jivesoftware.smack.packet.Message;
  6 import org.jivesoftware.smack.packet.Presence;
  7 import org.jivesoftware.smack.ConnectionConfiguration;
  8 import org.jivesoftware.smack.MessageListener;
  9 import org.jivesoftware.smack.Roster;
 10 import org.jivesoftware.smack.RosterEntry;
 11 import org.jivesoftware.smack.RosterListener;
 12 import org.jivesoftware.smack.XMPPConnection;
 13 import org.jivesoftware.smack.XMPPException;
 14 
 15 
 16 public class ChatClient implements MessageListener
 17 {
 18     XMPPConnection connection;
 19     
 20     public void login(String userName, String password) throws XMPPException
 21     {
 22         ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com"5222"gmail.com");
 23         connection = new XMPPConnection(config);
 24 
 25         connection.connect();
 26         connection.login(userName, password);
 27     }
 28     
 29     public void sendMessage(String message, String to) throws XMPPException
 30     {
 31         Chat chat = connection.getChatManager().createChat(to, this);
 32         chat.sendMessage(message);
 33     }
 34     
 35     public void displayBuddyList()
 36     {
 37         Roster roster = connection.getRoster();
 38         roster.addRosterListener(new RosterListener() {
 39             // Ignored events public void entriesAdded(Collection<String> addresses) {}
 40             public void entriesDeleted(Collection<String> addresses) {}
 41             public void entriesUpdated(Collection<String> addresses) {}
 42             public void presenceChanged(Presence presence) {
 43                 System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
 44             }
 45             @Override
 46             public void entriesAdded(Collection<String> arg0) {
 47                 // TODO Auto-generated method stub
 48                 System.out.println("New Buddy Comes!");
 49                 for (String x :arg0)
 50                     System.out.println("Buddy: " + x + "Added");                    
 51             }
 52             
 53         });
 54 
 55         Collection<RosterEntry> entries = roster.getEntries();
 56         
 57         System.out.println("\n\n" + entries.size() + " buddy(ies):");
 58         for(RosterEntry r:entries)
 59         {
 60             System.out.println(r.getUser() + " " + r.getName());
 61         }
 62     }
 63     
 64     public void deleteBuddy(String user) throws XMPPException
 65     {
 66         Roster roster = connection.getRoster();
 67         Collection<RosterEntry> entries = roster.getEntries();
 68         for (RosterEntry r:entries)
 69         {
 70             if (user.equals( r.getUser() ) )
 71                 {
 72                 roster.removeEntry(r);
 73                 break;
 74                 }
 75         }
 76 
 77     }
 78     
 79     public void createBuddy(String user, String name, String[] groups) throws XMPPException
 80     {
 81         Roster roster = connection.getRoster();
 82         roster.createEntry(user, name, groups);
 83         
 84     }
 85     
 86     public void disconnect()
 87     {
 88         connection.disconnect();
 89     }
 90     
 91     public void processMessage(Chat chat, Message message) 
 92     {
 93         if(message.getType() == Message.Type.chat)
 94             System.out.println(chat.getParticipant() + " says: " + message.getBody() + "\r\n");
 95     }
 96     
 97     public static void main(String args[]) throws XMPPException, IOException
 98     {
 99         // declare variables
100         ChatClient c = new ChatClient();
101         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
102         String msg;
103 
104 
105         // turn on the enhanced debugger
106         XMPPConnection.DEBUG_ENABLED = true;
107 
108 
109         // provide your login information here
110         c.login("username""password");
111 
112 
113         c.displayBuddyList();
114         System.out.println("-----");
115         System.out.println("Enter your message in the console.");
116         System.out.println("-----\n");
117         
118 
119         while!(msg=br.readLine()).equals("bye"))
120         {
121             // your buddy's gmail address goes here
122             c.sendMessage(msg, "littleds@jabber.org");
123         }
124 
125         
126         c.disconnect();
127         
128         System.exit(0);
129     }
130 }

基本是从Smack的Doc里面抄出来的,然后自己简单的组合了一下

实现了几个最基本的功能,一个是查看buddy list列表,一个是聊天,一个是更新好友状态

没有图形界面,因为不会Java GUI

自己创建了两个jabber.org的帐号测试

PS:pidgin很好用,画面简介,虽然有时候会崩溃,不过也没出现几次……