一步一步调通Openfire3.10.2+smack4.1.4官方示例代码

一步一步调通Openfire3.10.2+smack4.1.4官方示例代码

首先在电脑上安装Openfire服务器(本文安装的版本为Openfire 3.10.2)

Openfire下载地址http://www.igniterealtime.org/downloads/index.jsp
本文Openfire服务器名称为gwcheng-pc
Smack 4.1.4的官方文档下载地址包括jar包和javadoc(官方给的解释Easy to use Java XMPP client library.)
http://www.igniterealtime.org/downloads/download-landing.jsp?file=smack/smack_4_1_4.zip

下载Smack之后从文档smack_4_1_4/releasedocs/documentation/index.html给的示例

    // Create a connection to the gwcheng-pc server on a specific port.
    XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
              .setUsernameAndPassword("cheng", "cheng")
              .setServiceName("gwcheng-pc")
              .setHost("gwcheng-pc")
              .build();
            AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
            conn2.connect();

建立一个java工程,拷贝smack_4_1_4\libs\下的jar包到自己的工程里。
运行上面的代码(代码中”cheng”是我在openfire中注册的用户名和密码”gwcheng-pc”为我的openfire服务器名称 )
运行上面的程序,会报如下的错误

    Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
    Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory

这个错误很明显是缺少jar包 xmlpull.jar,此处导入xmlpull-1.1.3.1.jar
导入之后运行还是会报错

    Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.IllegalStateException: org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available
    Caused by: org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available

这种错误还是缺少jar包 kxml.jar,此处导入kxml2-2.3.0.jar
导入之后运行还是会报错

    Exception in thread "main" java.lang.NoClassDefFoundError: de/measite/minidns/DNSCache
    Caused by: java.lang.ClassNotFoundException: de.measite.minidns.DNSCache

继续导入jar包 minidns.jar,此处导入minidns-0.1.7.jar
导入之后运行还是会报错

    Exception in thread "main" java.lang.NoClassDefFoundError: org/jxmpp/util/cache/ExpirationCache
    Caused by: java.lang.ClassNotFoundException: org.jxmpp.util.cache.ExpirationCache

继续导入jar包 jxmpp-core-0.5.0-alpha6.jar和 jxmpp-util-cache-0.4.0-alpha1.jar
导入之后运行还是会报错

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/StrictHostnameVerifier
    Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.ssl.StrictHostnameVerifier

继续导入jar包 httpclient-4.4.1.jar
导入之后运行还是会报错

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

继续导入jar包 commons-logging-1.2.jar
导入之后运行还是会报错

org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

这个错误最严重,到目前我也没有找到合适的解决方法,试过了添加本地证书什么的都不行

无赖之下只有改变链接方式

        AbstractXMPPConnection connection = null;
        Builder builder = XMPPTCPConnectionConfiguration.builder();
        builder.setSecurityMode(SecurityMode.disabled);
        XMPPTCPConnectionConfiguration config = builder
            .setServiceName("gwcheng-pc")
            .setHost("gwcheng-pc").setPort(5222)
            .build();
        connection = new XMPPTCPConnection(config);
        connection.connect();
        connection.login("cheng", "cheng");

只有这样才能成功连接到openfire服务器,并登录,上述代码中的”gwcheng-pc”是openfire服务器名称,”cheng”是我在openfire上创建的用户的用户名和密码,都为”cheng”。

至此Openfire3.10.2+smack4.1.4的连接建立成功了,下一步发送并接受消息。
    conn.connect();
    conn.login("cheng", "cheng");
    // 发送消息
    //Assume we've created an XMPPConnection name "conn"
    ChatManager chatmanager =ChatManager.getInstanceFor(conn);
    Chat newChat = chatmanager.createChat("man@gwcheng-pc");
    newChat.sendMessage("我是程序员");

其中”man@gwcheng-pc”是openfire服务器中注册的另一个用户的jid

发送消息官方文档说明

The Chat.sendMessage(String) method is a convenience method that creates a Message object, sets the body using the String parameter, then sends the message.

发送完消息之后开始写接收消息的代码
try {
    AbstractXMPPConnection conn = GetXMPPConnection.getConnection();
    conn.connect();
    conn.login("man", "man");

    ChatManager chatmanager = ChatManager.getInstanceFor(conn);
    System.out.println("等待接受消息...");
    chatmanager.addChatListener(new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean create) {
            chat.addMessageListener(new ChatMessageListener() {
            @Override
            public void processMessage(Chat chat, Message msg) {
                if (null != msg.getBody()) {
                    System.out.println("接收到新消息:" + msg.getBody());
                            }
                        }
                    });
                }
            });
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        while (true);

完整示例代码下载地址
https://github.com/peer44/smark4

参考文献

http://www.oschina.net/code/snippet_2313055_48750

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值