一个opoenfire与smack的例子

假设openfire是开着的,这样下面两个类才可以跑起来
(附件包含了Smack帮助文档中文版和需要的jar包)

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

public class OpenFireUser {
	
	private XMPPConnection connection=new OpenFireConnection().getConnection();
	public String serverName="@"+connection.getServiceName();
	private String username = "";
	private String password = "";
	
	Roster roster ;
	Presence presence = new Presence(Presence.Type.subscribe);
	
	public OpenFireUser() {
	}
	
	public OpenFireUser(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	
	
	public boolean create() {//创建用户
		boolean b=true;
		try {
			connection.getAccountManager().createAccount(this.username, this.password);
			
		} catch (Exception e) {
			b=false;
		}
		return b;
	}
	
	public boolean create(Map m) {//创建用户
		boolean b=true;
		try {
			connection.getAccountManager().createAccount(this.username, this.password,m);
		} catch (Exception e) {
			b=false;
		}
		return b;
	}
	public boolean delete() {//删除用户
		boolean b=true;
		try {
			connection.getAccountManager().deleteAccount();
		} catch (Exception e) {
			b=false;
		}
		return b;
	}
	
	public boolean login() {//登陆
		
		boolean b=true;
		try {
			connection.login(this.username, this.password);
			roster = connection.getRoster();
			roster.addRosterListener(new RosterListener(){//监听Roster

				@Override
				public void entriesAdded(Collection<String> collection) {//添加好友的时候触发
					String user = "";
					Iterator<String> it = collection.iterator();
					if(it.hasNext()){
						user=it.next()+serverName;
					}
					
					//订阅好友的Presence
					Presence presence = new Presence(Presence.Type.subscribe); 
					presence.setTo(user);
					//presence.setMode(Presence.Mode.available); 
					connection.sendPacket(presence);
					
				}

				@Override
				public void entriesDeleted(Collection<String> arg0) {}

				@Override
				public void entriesUpdated(Collection<String> arg0) {}

				@Override
				public void presenceChanged(Presence p) {
					//当订阅的好友presence改变的时候调用
					//System.out.println("Presence changed: " + p.getFrom());
				}
				
			});
		
		} catch (Exception e) {
			b=false;
		}
		return b;
	}
	public boolean addFriends(String user) {
		return addFriends(user+serverName,"");
	}
	public boolean addFriends(String user,String nickname) {
		return addFriends(user, nickname,new String[]{"Firends"});
	}
	
	//添加一个好友,添加到Firends组上
	public boolean addFriends(String user,String nickname,String[] s) {
		boolean b=true;
		roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
		try {
			roster.createEntry(user,nickname,s);
		} catch (Exception e) {
			b=false;
		}
		return b;
	}
	
	
	public HashMap getFriends() {//获得好友
		return getFriends("");
	}
	public HashMap getFriends(String s) {//获得好友
		HashMap hm=new HashMap();
		Collection<RosterEntry> m=roster.getEntries();
		for(Iterator<RosterEntry> i=m.iterator();i.hasNext();){
			RosterEntry re= i.next();
			System.out.println("name:"+re.getName());//打印好友信息
			System.out.println("user:"+re.getUser());
			
		}

		return hm;
	}
	
	public boolean deleteFriend(String friendName){//删除某个好友
		boolean b=true;
		//roster=connection.getRoster();
		try {
			roster.removeEntry(roster.getEntry(friendName));
		} catch (Exception e) {
			b=false;
		}
		return b;
	}
	

	public void testPresences(String name){//看JID为name好友在不在线
		
		System.out.println(roster.getPresence(name));
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public static void main(String[] args) {
		
		OpenFireUser test2=new OpenFireUser("test2","test2");		
		OpenFireUser test1=new OpenFireUser("test1","test1");
		//ofu.create();//创建test1用户
		//ofu2.create();//创建test2用户
		
		test1.login();
		test2.login();
		
		try{
			Thread.sleep(1000);
		}catch (Exception e) {
			// TODO: handle exception
		}
		
		System.out.println("*******************test1**********************");
		//test1.addFriends("test2");//test1添加test2为好友,组为Friends
		//test1.testPresences();
		//test1.getFriends();//这里可以打印出好友
		try{
			//Thread.sleep(1000);
		}catch (Exception e) {
			// TODO: handle exception
		}
		System.out.println("*******************test2**********************");
		//test2.getFriends();//这里可以打印出好友
		//test2.addFriends("test1");
		try{
			//Thread.sleep(1000);
		}catch (Exception e) {
			// TODO: handle exception
		}
		test2.testPresences("test1@rd-test-1");//这里可以打印出好友是否在线
		test1.testPresences("test2@rd-test-1");
		
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}

}



import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
//这类只是连接openfire服务器,其他什么都不做
public class OpenFireConnection {
	
	String openFireServ="localhost";//openfire服务器这是在本机上,不然就是openfire服务器的IP地址
	
	public OpenFireConnection() {
		super();
	}
	
	public OpenFireConnection(String openFireServ) {
		super();
		this.openFireServ = openFireServ;
	}
	
	public XMPPConnection getConnection() {
		ConnectionConfiguration config = new ConnectionConfiguration(openFireServ, 5222);   
	    XMPPConnection conn = null;   
	    try {
	    	conn=new XMPPConnection(config);
			conn.connect();
		} catch (XMPPException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public String getOpenFireServ() {
		return openFireServ;
	}

	public void setOpenFireServ(String openFireServ) {
		this.openFireServ = openFireServ;
	}
}

你可能感兴趣的:(thread)