运行环境:JDK1.4+
第三方包:Smack(Openfire服务器官方提供)
XMPP服务器:Openfire 3.7.0
特点:可直接与QQ,MSN,Gtalk等账号绑定,可直接与QQ,Gtalk,MSN等聊天工具互通。
通过这个Java程序,让大家首先先了解一下基于XMPP协议的即时通信的基本原理。
Java核心代码:
- 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;
- /**
- * 构造 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();
- }
- }
-
- /**
- * 构造 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();
- }
- }
-
- /**
- * 构造 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;
- }
-
- /**
- * 返回相应(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;
- }
- }
转自:http://cxlh.iteye.com/blog/246973
阅读(255) | 评论(0) | 转发(0) |