Chinaunix首页 | 论坛 | 博客
  • 博客访问: 56660
  • 博文数量: 49
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-11 10:27
个人简介

潜行着!

文章分类

全部博文(49)

文章存档

2014年(49)

我的朋友

分类: Java

2014-05-11 11:15:36

运行环境:JDK1.4+

第三方包:Smack(Openfire服务器官方提供)

XMPP服务器:Openfire 3.7.0

特点:可直接与QQ,MSN,Gtalk等账号绑定,可直接与QQ,Gtalk,MSN等聊天工具互通。

通过这个Java程序,让大家首先先了解一下基于XMPP协议的即时通信的基本原理。

Java核心代码:

  1. package com.nbhj.im;
  2.   
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.   
  8. import org.jivesoftware.smack.ConnectionConfiguration;
  9. import org.jivesoftware.smack.Roster;
  10. import org.jivesoftware.smack.RosterEntry;
  11. import org.jivesoftware.smack.RosterGroup;
  12. import org.jivesoftware.smack.XMPPConnection;
  13. import org.jivesoftware.smack.XMPPException;
  14.   
  15.   
  16.   
  17. public class IMServer {
  18.   
  19.     private ConnectionConfiguration connectionConfig;
  20.     private XMPPConnection connection;
  21.     private Roster roster;
  22.     private boolean loginState;
  23.     private Listener listener;
  24.     /**
  25.      * 构造 IMServer(serviceName)
  26.      */
  27.     public IMServer(String serviceName) {
  28.         connectionConfig = new ConnectionConfiguration(serviceName);
  29.         connection = new XMPPConnection(connectionConfig);
  30.         listener=new Listener();
  31.         try {
  32.             connection.connect();
  33.         } catch (XMPPException e) {
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.   
  38.     /**
  39.      * 构造 IMServer(host,port)
  40.      */
  41.     public IMServer(String host, String port) {
  42.   
  43.         connectionConfig = new ConnectionConfiguration(host, Integer
  44.                 .parseInt(port));
  45.         connection = new XMPPConnection(connectionConfig);
  46.         listener=new Listener();
  47.         try {
  48.             connection.connect();
  49.         } catch (XMPPException e) {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53.   
  54.     /**
  55.      * 构造 IMServer(host port serviceName)
  56.      */
  57.     public IMServer(String host, int port, String serviceName) {
  58.         connectionConfig = new ConnectionConfiguration(host, port, serviceName);
  59.         connection = new XMPPConnection(connectionConfig);
  60.         listener=new Listener();
  61.            
  62.         try {
  63.             connection.connect();
  64.                
  65.         } catch (XMPPException e) {
  66.             e.printStackTrace();
  67.         }
  68.     }
  69.   
  70.     /**
  71.      * 账户登陆 Server
  72.      *
  73.      * @return boolean
  74.      */
  75.     public boolean loginServer(String userName, String userPswd) {
  76.         try {
  77.             connection.login(userName, userPswd);
  78.             loginState = true;
  79.             roster = connection.getRoster();
  80.                
  81.             listener.regConnectionListener(connection);
  82.             listener.regPackListener(connection);
  83.             listener.onlineServer(connection);
  84.             listener.regRosterListener(roster);
  85.         } catch (XMPPException e) {
  86.             e.printStackTrace();
  87.         }
  88.         return loginState;
  89.     }
  90.        
  91.        
  92.   
  93.     /**
  94.      * 注册新账号
  95.      *
  96.      * @return boolean
  97.      */
  98.     public boolean createAccount(String regUserName, String regUserPswd) {
  99.         try {
  100.             connection.getAccountManager().createAccount(regUserName,
  101.                     regUserPswd);
  102.             return true;
  103.         } catch (XMPPException e) {
  104.             e.printStackTrace();
  105.             return false;
  106.         }
  107.   
  108.     }
  109.   
  110.     /**
  111.      * 账户退出 Server
  112.      *
  113.      * @return boolean
  114.      */
  115.     public boolean logoutServer() {
  116.         if (loginState) {
  117.             connection.disconnect();
  118.             listener.stopOnlineThread();
  119.             loginState = false;
  120.         }
  121.         return !loginState;
  122.     }
  123.   
  124.     /**
  125.      * 返回所有用户信息 <RosterEntry>
  126.      *
  127.      * @return List(RosterEntry)
  128.      */
  129.     public List<RosterEntry> getOnlineEntries() {
  130.         List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
  131.         Collection<RosterEntry> rosterEntry = roster.getEntries();
  132.         Iterator<RosterEntry> i = rosterEntry.iterator();
  133.         while (i.hasNext()){
  134.            
  135.             EntriesList.add(i.next());
  136.         }
  137.         return EntriesList;
  138.     }
  139.     /**
  140.      * 返回所有用户信息 <RosterEntry>
  141.      *
  142.      * @return List(RosterEntry)
  143.      */
  144.     public List<RosterEntry> getAllEntries() {
  145.         List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
  146.         Collection<RosterEntry> rosterEntry = roster.getEntries();
  147.         Iterator<RosterEntry> i = rosterEntry.iterator();
  148.         while (i.hasNext())
  149.             EntriesList.add(i.next());
  150.         return EntriesList;
  151.     }
  152.   
  153.     /**
  154.      * 返回相应(groupName)组里的所有用户<RosterEntry>
  155.      *
  156.      * @return List(RosterEntry)
  157.      */
  158.     public List<RosterEntry> getEntriesByGroup(String groupName) {
  159.         List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
  160.         RosterGroup rosterGroup = roster.getGroup(groupName);
  161.         Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
  162.         Iterator<RosterEntry> i = rosterEntry.iterator();
  163.         while (i.hasNext())
  164.             EntriesList.add(i.next());
  165.         return EntriesList;
  166.     }
  167.   
  168.     /**
  169.      * 返回所有组信息 <RosterGroup>
  170.      *
  171.      * @return List(RosterGroup)
  172.      */
  173.     public List<RosterGroup> getGroups() {
  174.         List<RosterGroup> groupsList = new ArrayList<RosterGroup>();
  175.         Collection<RosterGroup> rosterGroup = roster.getGroups();
  176.         Iterator<RosterGroup> i = rosterGroup.iterator();
  177.         while (i.hasNext())
  178.             groupsList.add(i.next());
  179.         return groupsList;
  180.     }
  181.   
  182.     /**
  183.      * @return connection
  184.      */
  185.     public XMPPConnection getConnection() {
  186.         return connection;
  187.     }
  188.   
  189.     /**
  190.      * @return loginState
  191.      */
  192.     public boolean getLoginState() {
  193.         return loginState;
  194.     }
  195.   
  196.     /**
  197.      * @return roster
  198.      */
  199.     public Roster getRoster() {
  200.         return roster;
  201.     }
  202. }

转自:http://cxlh.iteye.com/blog/246973

阅读(232) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~