運行環境:JDK1.4+
第三方包:Smack(Openfire服務器官方提供)
XMPP服務器:Openfire 3.6
特點:可直接與QQ,MSN,Gtalk等賬號綁定,可直接與QQ,Gtalk,MSN等聊天工具互通
通過這個Java程序,讓大家首先先了解一下基于XMPP協議的即時通信的基本原理,希望大家通過界面上的報文了解通信的遠離,我先拋磚引玉一下,
核心源碼:
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;
}
}
posted on 2008-12-09 13:28
墻頭草 閱讀(4943)
評論(5) 編輯 收藏