Smack4.1.1 简单通信

  • 客户端A
import java.io.IOException;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder;

public class Test01 {
	/**
	 * @param args
	 * @throws XMPPException 
	 * @throws IOException 
	 * @throws SmackException 
	 */
	public static void main(String[] args) throws SmackException, IOException, XMPPException {
		Builder builder = XMPPTCPConnectionConfiguration.builder();
		builder.setServiceName("chenph-pc");
		builder.setHost("localhost");
		builder.setPort(5222);
		builder.setUsernameAndPassword("chenph", "123456");
		builder.setSecurityMode(SecurityMode.disabled);// 不支持TLS验证
		
		AbstractXMPPConnection connection = new XMPPTCPConnection(
				builder.build());
		connection.connect();
		connection.login();
		ChatManager chatManager = ChatManager.getInstanceFor(connection);
		chatManager.addChatListener(
			new ChatManagerListener() {
				@Override
				public void chatCreated(Chat chat, boolean createdLocally){
					if (!createdLocally)
						chat.addMessageListener(new MyNewMessageListener());;
				}
			});
		while(true);
	}
}
  • 客户端B
import java.io.IOException;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder;

public class Test02 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Builder builder = XMPPTCPConnectionConfiguration.builder();
		builder.setServiceName("chenph-pc");
		builder.setHost("localhost");
		builder.setPort(5222);
		builder.setUsernameAndPassword("admin", "123456");
		builder.setSecurityMode(SecurityMode.disabled);// 不支持TLS验证

		AbstractXMPPConnection connection = new XMPPTCPConnection(
				builder.build());

		try {
			connection.connect();
			
			connection.login();
			//System.out.println(connection.getUser());
			ChatManager chatmanager = ChatManager.getInstanceFor(connection);
			Chat newChat = chatmanager.createChat("chenph@chenph-pc/Smack", new ChatMessageListener() {
				public void processMessage(Chat chat, Message message) {
					System.out.println("Received message: " + message);
				}
			});

			newChat.sendMessage("Howdy!");
			
			while(true);
		} catch (SmackException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XMPPException e) {
			e.printStackTrace();
		}
	}
}
  • 原文地址:http://blog.csdn.net/yueritian/article/details/48522433


你可能感兴趣的:(smack)