- 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;
- }
- }
smack操作openfire的API介绍(2)
最新推荐文章于 2018-06-26 16:45:17 发布