openfire开发指南《连接和登陆》

第一步
    官网下载smack.jar包
    下载地址:http://www.igniterealtime.org/downloads/index.jsp#smack
第二步
    把smack里边的jar导入你新建的java项目中

开始编写smack连接openfire代码

package openfireTest;

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class SmackXMPTest {

private Connection connection;

private ConnectionConfiguration config;

         //openfire 连接地址
private final static String server = "127.0.0.1";

public final void fail(Object o,Object... args){

if(o!=null&&args!=null&&args.length>0){
String s = o.toString();
for(int i=0;i<args.length;i++){
String item = args[i] == null ?"":args[i].toString();
s +="" + item;
}

System.out.println(s);
}


}




/**
* 初始化连接
*/
public void init(){
                  //第一个参数openfire地址,第二个参数端口号
config = new ConnectionConfiguration(server, 5222);
connection = new XMPPConnection(config);
try {
connection.connect();
                          //连接成功
} catch (XMPPException e) {
//连接失败
                       e.printStackTrace();
}
}
//测试登陆
public void testLogin(){

try {
connection.login("zhao\\40china.com", "1");
                     //登陆成功
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


public void destory(){
if(connection != null){
connection.disconnect();
connection = null;
}
}

public static void main(String[] args) {
SmackXMPTest test = new SmackXMPTest();
test.init();
test.testLogin();

}

}

你可能感兴趣的:(openfire,开发指南,smack)