`
ssxxjjii
  • 浏览: 931916 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

openFire客户端编程例子

    博客分类:
  • IM
 
阅读更多
Java代码  收藏代码
  1. import java.util.Collection;  
  2.    
  3. import org.jivesoftware.smack.packet.Message;  
  4. import org.jivesoftware.smack.packet.Presence;  
  5.    
  6. public class SimpleClient {  
  7.    
  8.     /** Server name */  
  9.     public static final String SERVER_NAME = "localhost";  
  10.     /** Server port */  
  11.     public static final int SERVER_PORT = 5222// as you can see in Admin  
  12.                                                 // Console properties  
  13.    
  14.     /** Client name - for login */  
  15.     private final String clientName;  
  16.     /** Client password - for login*/  
  17.     private final String clientPassword;  
  18.     /** Client color - for writing in color */  
  19.     private final String clientColor;  
  20.     /** Chat friend */  
  21.     private final String friendName;  
  22.    
  23.     /** 
  24.      * Constrcuts a new SimpleClient 
  25.      * @param clientName0 - 
  26.      * @param clientPassword0 - 
  27.      * @param clientColor0 - 
  28.      * @param friendName0 - 
  29.      */  
  30.     public SimpleClient(String clientName0, String clientPassword0, String clientColor0, String friendName0) {  
  31.         super();  
  32.         this.clientName = clientName0;  
  33.         this.clientPassword = clientPassword0;  
  34.         this.clientColor = clientColor0;  
  35.         this.friendName = friendName0;  
  36.     }  
  37.    
  38.     /** 
  39.      * Main process. 
  40.      */  
  41.     public void run() {  
  42.         try {  
  43.             this.runImpl();  
  44.         } catch (XMPPException e) {  
  45.             System.err.println("Exception: " + e);  
  46.         }  
  47.    
  48.     }  
  49.    
  50.     private void runImpl() throws XMPPException {  
  51.             XMPPConnection.DEBUG_ENABLED = true;  
  52.    
  53.             /* 
  54.              * Configuration 
  55.              */  
  56.             final ConnectionConfiguration config = new ConnectionConfiguration(  
  57.                     SimpleClient.SERVER_NAME, SimpleClient.SERVER_PORT);  
  58.             config  
  59.                     .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);  
  60.             config.setCompressionEnabled(false);  
  61.    
  62.    
  63.             /* 
  64.              * Open Connection 
  65.              */  
  66.             final XMPPConnection connection = new XMPPConnection(config);  
  67.             connection.connect();  
  68.             connection.login(this.clientName, this.clientPassword);  
  69.    
  70.             /* 
  71.              * Chat 
  72.              */  
  73.             final MessageListener messageListner = new SimpleMessageListner();  
  74.             final Chat chat = connection.getChatManager().createChat(this.friendName, messageListner);  
  75.             final java.util.Date date = new java.util.Date(System.currentTimeMillis());  
  76.             final String msgSent = " [sent by " + this.clientName + ", at " + date.toString() + "]";  
  77.             final Message newMessage = new Message();  
  78.             newMessage.setBody("Howdy (colored)!" + msgSent);  
  79.             newMessage.setProperty("favoriteColor"this.clientColor);  
  80.             chat.sendMessage(newMessage);  
  81.             chat.sendMessage("Howdy!" + msgSent);  
  82.    
  83.             /* 
  84.              * Roster 
  85.              */  
  86.             final Roster roster = connection.getRoster();  
  87.             // this is already the default Mode but added just for the example  
  88.             Roster  
  89.                     .setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);  
  90.             roster.createEntry(this.friendName, ""null);  
  91.    
  92.             this.presenceStatus(roster, "unknown@someone");  
  93.             this.presenceStatus(roster, this.friendName);  
  94.    
  95.             final Collection<RosterEntry> entries = roster.getEntries();  
  96.             for (RosterEntry entry : entries) {  
  97.                 System.out.println("RosterEntry " + entry);  
  98.             }  
  99.    
  100.             final RosterListener rosterListener = new SimpleRosterListner();  
  101.             roster.addRosterListener(rosterListener);  
  102.    
  103.             /* 
  104.              * End connection 
  105.              */  
  106.             connection.disconnect();  
  107.     }  
  108.    
  109.     private void presenceStatus(final Roster roster, String user) {  
  110.         final Presence presence = roster.getPresence(user);  
  111.         final String response = presence == null ? "Not around" : presence.getStatus();  
  112.         System.out.println("Presence of '" + user + "': " + response);  
  113.     }  
  114.    
  115.     /** 
  116.      * 
  117.      * @author some 
  118.      * 
  119.      */  
  120.     private class SimpleMessageListner implements MessageListener {  
  121.             public void processMessage(Chat chat, Message message) {  
  122.                 System.out.println("Received message: "  
  123.                         + message.getBody());  
  124.             }  
  125.     }  
  126.    
  127.     /** 
  128.      * 
  129.      * @author some 
  130.      * 
  131.      */  
  132.     private class SimpleRosterListner implements RosterListener {  
  133.         // Ignored events  
  134.         public void entriesAdded(Collection<String> addresses) {  
  135.         }  
  136.    
  137.         public void entriesDeleted(Collection<String> addresses) {  
  138.         }  
  139.    
  140.         public void entriesUpdated(Collection<String> addresses) {  
  141.         }  
  142.    
  143.         public void presenceChanged(Presence presence) {  
  144.             System.out.println("Presence changed: "  
  145.                     + presence.getFrom() + " " + presence);  
  146.         }  
  147.    
  148.     }  
  149.    

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics