tigase 的基本使用 (smack)

连接管理类
public class GetXMPPConnection {
    //获取连接
    public static AbstractXMPPConnection  getConnection(){
        AbstractXMPPConnection connection;
        Builder builder = XMPPTCPConnectionConfiguration.builder();
        builder.setSecurityMode(SecurityMode.disabled);
        XMPPTCPConnectionConfiguration config = builder.setServiceName("admin-PC").setHost("192.168.1.168").setPort(5222)
                .build();
        connection = new XMPPTCPConnection(config);
        return connection;
    }
    //断开连接
    public static void closeConnection(AbstractXMPPConnection connection){
        if (connection != null) {
            try {
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
注册
public class register {

    public static void main(String[] args) throws IOException, XMPPException, SmackException {
        String account = "test";
        String password = "123456";

        AbstractXMPPConnection conn = GetXMPPConnection.getConnection();
        conn.connect();

        try {
            AccountManager.sensitiveOperationOverInsecureConnectionDefault(true);
            AccountManager.getInstance(conn).createAccount(account, password);
        } catch (XMPPException | SmackException e) {
            e.printStackTrace();
        }finally {
            GetXMPPConnection.closeConnection(conn);
            System.out.println("断开了连接");
        }

    }

}
登陆
public class login {
    public static void main(String[] args) {
        AbstractXMPPConnection conn = null;
        String account = "java";
        String password = "123456";
        try {
            // 登录
            conn = GetXMPPConnection.getConnection();
            conn.connect();
            conn.login(account, password);
        } catch (Exception e) {
            // SASLError using SCRAM-SHA-1: not-authorized 密码错或者用户不存在都是这个
            e.printStackTrace();
        } finally {
            GetXMPPConnection.closeConnection(conn);
        }
    }
}
发送消息
public class SendMessage{
    public static void main(String[] args) {
        String account = "java";
        String password = "123456";
        AbstractXMPPConnection conn = null;
        try {
            conn = GetXMPPConnection.getConnection();
            conn.connect();
            conn.login(account, password);
            // 发送消息
            ChatManager chatmanager = ChatManager.getInstanceFor(conn);
            Chat newChat = chatmanager.createChat("java3@admin-PC");
            newChat.sendMessage("java3 , my name is java");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            GetXMPPConnection.closeConnection(conn);;
        }
    }

}
接收消息
public class ReceiveMessage{
    public static void main(String[] args) throws IOException, SmackException, XMPPException {
        String account = "java";
        String password = "123456";
        AbstractXMPPConnection conn = GetXMPPConnection.getConnection();
        conn.connect();
        conn.login(account, password);

        ChatManager chatmanager = ChatManager.getInstanceFor(conn);
        System.out.println("等待接受消息...");

        chatmanager.addChatListener((chat, create) -> chat.addMessageListener((chat1, msg) -> {
            if (null != msg.getBody()) {
                String friendName = msg.getFrom().split("@")[0];
                String content = msg.getBody();
                System.out.println(friendName +" :" + content);
            }
        }));
        while (true);
    }
}

你可能感兴趣的:(tigase 的基本使用 (smack))