posts - 241,  comments - 116,  trackbacks - 0

          運行環(huán)境:JDK1.4+

          第三方包:Smack(Openfire服務(wù)器官方提供)

          XMPP服務(wù)器:Openfire 3.6

          特點:可直接與QQ,MSN,Gtalk等賬號綁定,可直接與QQ,Gtalk,MSN等聊天工具互通

          通過這個Java程序,讓大家首先先了解一下基于XMPP協(xié)議的即時通信的基本原理,希望大家通過界面上的報文了解通信的遠(yuǎn)離,我先拋磚引玉一下,

          核心源碼:

          package com.nbhj.im;

          import java.util.ArrayList;
          import java.util.Collection;
          import java.util.Iterator;
          import java.util.List;

          import org.jivesoftware.smack.ConnectionConfiguration;
          import org.jivesoftware.smack.Roster;
          import org.jivesoftware.smack.RosterEntry;
          import org.jivesoftware.smack.RosterGroup;
          import org.jivesoftware.smack.XMPPConnection;
          import org.jivesoftware.smack.XMPPException;



          public class IMServer {

              
          private ConnectionConfiguration connectionConfig;
              
          private XMPPConnection connection;
              
          private Roster roster;
              
          private boolean loginState;
              
          private Listener listener;
              
          /**
               * 構(gòu)造 IMServer(serviceName)
               
          */
              
          public IMServer(String serviceName) {
                  connectionConfig 
          = new ConnectionConfiguration(serviceName);
                  connection 
          = new XMPPConnection(connectionConfig);
                  listener
          =new Listener();
                  
          try {
                      connection.connect();
                  } 
          catch (XMPPException e) {
                      e.printStackTrace();
                  }
              }

              
          /**
               * 構(gòu)造 IMServer(host,port)
               
          */
              
          public IMServer(String host, String port) {

                  connectionConfig 
          = new ConnectionConfiguration(host, Integer
                          .parseInt(port));
                  connection 
          = new XMPPConnection(connectionConfig);
                  listener
          =new Listener();
                  
          try {
                      connection.connect();
                  } 
          catch (XMPPException e) {
                      e.printStackTrace();
                  }
              }

              
          /**
               * 構(gòu)造 IMServer(host port serviceName)
               
          */
              
          public IMServer(String host, int port, String serviceName) {
                  connectionConfig 
          = new ConnectionConfiguration(host, port, serviceName);
                  connection 
          = new XMPPConnection(connectionConfig);
                  listener
          =new Listener();
                  
                  
          try {
                      connection.connect();
                      
                  } 
          catch (XMPPException e) {
                      e.printStackTrace();
                  }
              }

              
          /**
               * 賬戶登陸 Server
               * 
               * 
          @return boolean
               
          */
              
          public boolean loginServer(String userName, String userPswd) {
                  
          try {
                      connection.login(userName, userPswd);
                      loginState 
          = true;
                      roster 
          = connection.getRoster();
                      
                      listener.regConnectionListener(connection);
                      listener.regPackListener(connection);
                      listener.onlineServer(connection);
                      listener.regRosterListener(roster);
                  } 
          catch (XMPPException e) {
                      e.printStackTrace();
                  }
                  
          return loginState;
              }
              
              

              
          /**
               * 注冊新賬號
               * 
               * 
          @return boolean
               
          */
              
          public boolean createAccount(String regUserName, String regUserPswd) {
                  
          try {
                      connection.getAccountManager().createAccount(regUserName,
                              regUserPswd);
                      
          return true;
                  } 
          catch (XMPPException e) {
                      e.printStackTrace();
                      
          return false;
                  }

              }

              
          /**
               * 賬戶退出 Server
               * 
               * 
          @return boolean
               
          */
              
          public boolean logoutServer() {
                  
          if (loginState) {
                      connection.disconnect();
                      listener.stopOnlineThread();
                      loginState 
          = false;
                  }
                  
          return !loginState;
              }

              
          /**
               * 返回所有用戶信息 <RosterEntry>
               * 
               * 
          @return List(RosterEntry)
               
          */
              
          public List<RosterEntry> getOnlineEntries() {
                  List
          <RosterEntry> EntriesList = new ArrayList<RosterEntry>();
                  Collection
          <RosterEntry> rosterEntry = roster.getEntries();
                  Iterator
          <RosterEntry> i = rosterEntry.iterator();
                  
          while (i.hasNext()){
                  
                      EntriesList.add(i.next());
                  }
                  
          return EntriesList;
              }
              
          /**
               * 返回所有用戶信息 <RosterEntry>
               * 
               * 
          @return List(RosterEntry)
               
          */
              
          public List<RosterEntry> getAllEntries() {
                  List
          <RosterEntry> EntriesList = new ArrayList<RosterEntry>();
                  Collection
          <RosterEntry> rosterEntry = roster.getEntries();
                  Iterator
          <RosterEntry> i = rosterEntry.iterator();
                  
          while (i.hasNext())
                      EntriesList.add(i.next());
                  
          return EntriesList;
              }

              
          /**
               * 返回相應(yīng)(groupName)組里的所有用戶<RosterEntry>
               * 
               * 
          @return List(RosterEntry)
               
          */
              
          public List<RosterEntry> getEntriesByGroup(String groupName) {
                  List
          <RosterEntry> EntriesList = new ArrayList<RosterEntry>();
                  RosterGroup rosterGroup 
          = roster.getGroup(groupName);
                  Collection
          <RosterEntry> rosterEntry = rosterGroup.getEntries();
                  Iterator
          <RosterEntry> i = rosterEntry.iterator();
                  
          while (i.hasNext())
                      EntriesList.add(i.next());
                  
          return EntriesList;
              }

              
          /**
               * 返回所有組信息 <RosterGroup>
               * 
               * 
          @return List(RosterGroup)
               
          */
              
          public List<RosterGroup> getGroups() {
                  List
          <RosterGroup> groupsList = new ArrayList<RosterGroup>();
                  Collection
          <RosterGroup> rosterGroup = roster.getGroups();
                  Iterator
          <RosterGroup> i = rosterGroup.iterator();
                  
          while (i.hasNext())
                      groupsList.add(i.next());
                  
          return groupsList;
              }

              
          /**
               * 
          @return connection
               
          */
              
          public XMPPConnection getConnection() {
                  
          return connection;
              }

              
          /**
               * 
          @return loginState
               
          */
              
          public boolean getLoginState() {
                  
          return loginState;
              }

              
          /**
               * 
          @return roster
               
          */
              
          public Roster getRoster() {
                  
          return roster;
              }
          }


          posted on 2008-12-09 13:28 墻頭草 閱讀(4943) 評論(5)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運專家
          主站蜘蛛池模板: 太仆寺旗| 南漳县| 清水河县| 大庆市| 都兰县| 石狮市| 佛冈县| 凤庆县| 新巴尔虎右旗| 玉林市| 富顺县| 定南县| 商都县| 武隆县| 遂昌县| 巴彦县| 定边县| 泉州市| 上林县| 连南| 巴彦淖尔市| 曲麻莱县| 南昌县| 瓮安县| 宁陕县| 临江市| 东明县| 江北区| 余庆县| 聂荣县| 南郑县| 托克逊县| 如东县| 安图县| 福海县| 绵竹市| 五河县| 兴文县| 朝阳县| 白朗县| 临沂市|