Fuck XMPP (2) 用Smack实现Java客户端连接、发送文本

1、已搭建好XMPP server,比如OpenFire,并正在运行。


2、下载、导入smack.jar包,创建一个Java项目,main如下:

package test.xmpp.smack;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

public class Client {
	public static void main(String[] args) throws XMPPException {
		Connection connection = new XMPPConnection("127.0.0.1");
		connection.connect();
		connection.login("HAHA", "123");
		Chat chat = connection.getChatManager().createChat("HOHO@localhost", new MessageListener() {

			@Override
		    public void processMessage(Chat chat, Message message) {
		        System.out.println("Received message: " + message);
		    }

		});
		chat.sendMessage("Howdy!");
	}
}

运行,HOHO收到“Howdy”即正确。

你可能感兴趣的:(smack)