调用XMPPTCPConnection.login时抛出的SmackException $ NoResponseException(Smack-4.1.0)

时间:2015-04-07 18:17:38

标签: java xmpp smack

环境:

  • 服务器:OpenFire v3.9.3。
  • 客户端操作系统:Linux
  • 服务器操作系统:Linux
  • Java:1.8.0_40
  • Smack:smack-core-4.10,smack-im-4.1.0,smack-tcp-4.1.0,smack-sasl-provided-4.1.0

这是我的测试客户端代码:

public class XMPPClientTest
{
    public static void main(String[] args)
    {

        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setHost("xmpp.domain")
                .setServiceName("xmpp.domain")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setDebuggerEnabled(true)
                .setResource("Smack-client")
                .build();

        AbstractXMPPConnection conn1 = new XMPPTCPConnection(config);
        try
        {
            System.out.println("connecting");
            conn1.connect();
            System.out.println("connected");

            System.out.println("logging in");
            conn1.login("user", "password");
            System.out.println("logged in");

            ChatManager chatmanager = ChatManager.getInstanceFor(conn1);
            Chat newChat = chatmanager.createChat("user@xmpp.domain");

            newChat.sendMessage("Goodbye World!");
            conn1.disconnect();
        }
        catch (IOException | SmackException | XMPPException ioe)
        {
            ioe.printStackTrace();
        }
    }
}

这是输出

connecting
11:49:40 AM SENT (0): <stream:stream xmlns='jabber:client' to='xmpp.domain' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
11:49:40 AM RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="xmpp.domain" id="3d0a3800" xml:lang="en" version="1.0">
11:49:40 AM RECV (0): <stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism><mechanism>CRAM-MD5</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><auth xmlns="http://jabber.org/features/iq-auth"/></stream:features>
logging in
11:49:40 AM SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'>=</auth>
11:49:40 AM RECV (0): <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cmVhbG09InhtcHAua2lkY2hlY2suY29tIixub25jZT0iYXpqS2N3UUh3S2RjTTVQeWt4OEo2YmdsM1VoMk9JQkVTallBWXFLOSIscW9wPSJhdXRoIixjaGFyc2V0PXV0Zi04LGFsZ29yaXRobT1tZDUtc2Vzcw==</challenge>
org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: No filter used or filter was 'null'.
    at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:106)
    at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:85)
    at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:250)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginNonAnonymously(XMPPTCPConnection.java:365)
    at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:452)
    at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:427)
    at com.forge.label.app.driver.test.XMPPClientTest.main(XMPPClientTest.java:39)
Apr 07, 2015 11:49:45 AM org.jivesoftware.smack.AbstractXMPPConnection callConnectionClosedOnErrorListener
WARNING: Connection closed with error
java.lang.NullPointerException
    at org.jivesoftware.smack.util.stringencoder.Base64.decode(Base64.java:86)
    at org.jivesoftware.smack.sasl.SASLMechanism.challengeReceived(SASLMechanism.java:229)
    at org.jivesoftware.smack.SASLAuthentication.challengeReceived(SASLAuthentication.java:328)
    at org.jivesoftware.smack.SASLAuthentication.challengeReceived(SASLAuthentication.java:313)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1040)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)
    at java.lang.Thread.run(Thread.java:745)

我可以使用像Pidgin这样的IM客户端连接到服务器。但是当我尝试使用Smack库时,我得到了上述错误。我发现这个问题的唯一“解决方案”是调用XMPPTCPConnectionConfiguration.setSecurityMode(SecurityMode.disabled),但这在我的情况下没有用。

从调试输出看来服务器正在发送质询,客户端忽略它。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

就我而言,我在pom.xml中删除了以下的依赖

然后,该错误没有再出现。

    <dependency>
       <groupId>org.igniterealtime.smack</groupId>
       <artifactId>smack-extensions</artifactId>
       <version>4.1.4</version>
    </dependency>

答案 1 :(得分:-1)

当你调用connect()时,将建立使用smack的Xmpp连接。但它只是启动连接过程。将在后台建立连接(单独的线程)。

在您的情况下,您将在connect()语句之后登录xmpp服务器。到那时登录语句被执行,可能无法建立连接。因此,通过实现ConnectionListener接口使用回调方法进行连接,如下所示:

XMPPTCPConnection connection = new XMPPTCPConnection(config);
    connection.addConnectionListener(new ConnectionListener() {

        @Override
        public void reconnectionSuccessful() {
        }

        @Override
        public void reconnectionFailed(Exception arg0) {
        }

        @Override
        public void reconnectingIn(int arg0) {
        }

        @Override
        public void connectionClosedOnError(Exception arg0) {
        }

        @Override
        public void connectionClosed() {
        }

        @Override
        public void connected(XMPPConnection arg0) {
            System.out.println("Connected to xmpp service....");
            try {
                connection.login("username", "password");
                System.out.println("Logged in to xmpp service....");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void authenticated(XMPPConnection arg0, boolean arg1) {
            // TODO Auto-generated method stub

        }
    });
    connection.connect();