使用Smack的Quickblox的XMPP客户端

时间:2015-07-28 19:43:23

标签: xmpp smack quickblox

我试图编写一个XMPP客户端来连接到Quickblox并将其用作聊天应用程序的机器人。我为此目的使用Smack 4.1.3。这是我的代码:

public static void sendChat1() {

    XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
            .setUsernameAndPassword("4461610-26179", "pass")
            .setServiceName("chat.quickblox.com")
            .setPort(5222)
            .build();

    System.out.println("Establishing Connection");
    AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
    try {
        conn2.connect();
    } catch (SmackException e) {
        System.out.println("ERROR");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("ERROR");
        e.printStackTrace();
    } catch (XMPPException e) {
        System.out.println("ERROR");
        e.printStackTrace();
    }



    MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(conn2);

    System.out.println("Creating multi user chat room");
    MultiUserChat muc = manager.getMultiUserChat("26179_55b76303535c12544b00b550@muc.chat.quickblox.com");

    System.out.println("Joining chat room");
    try {
        muc.join("4461610");
    } catch (XMPPException.XMPPErrorException e) {
        System.out.println("ERROR");
        e.printStackTrace();
        return;
    } catch (SmackException e) {
        System.out.println("ERROR");
        e.printStackTrace();
        return;
    }

    System.out.println("Chat room request");
    try {
        muc.sendConfigurationForm(new Form(DataForm.Type.submit));
    } catch (SmackException.NoResponseException e) {
        System.out.println("ERROR");
        e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
        System.out.println("ERROR");
        e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
        System.out.println("ERROR");
        e.printStackTrace();
    }
}

    public static void main(String[] args) {
        sendChat1();
    }

出于某种原因,我无法使用MultiUserChat使客户端连接到聊天室。这是我运行此代码时的输出:

Establishing Connection
Creating multi user chat room
Joining chat room
ERROR
org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: not-authorized - auth
at                       org.jivesoftware.smack.XMPPException$XMPPErrorException.ifHasErrorThenThrow(XMPPException.java:135)
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:232)
at org.jivesoftware.smackx.muc.MultiUserChat.enter(MultiUserChat.java:311)
at org.jivesoftware.smackx.muc.MultiUserChat.join(MultiUserChat.java:495)
at org.jivesoftware.smackx.muc.MultiUserChat.join(MultiUserChat.java:430)
at Main.sendChat1(Main.java:53)
at Main.main(Main.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Jul 29, 2015 12:52:36 AM org.jivesoftware.smack.roster.Roster$PresencePacketListener processPacket
WARNING: Roster not loaded while processing presence stanza

Process finished with exit code 0

我打开调试并发现了这个:

Establishing Connection
01:28:55 AM SENT (0): <stream:stream xmlns='jabber:client' to='chat.quickblox.com' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='4461610-26179@chat.quickblox.com@chat.quickblox.com' xml:lang='en'>
01:28:56 AM RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='tigase-error-tigase' from='chat.quickblox.com' version='1.0' xml:lang='en'><stream:error><improper-addressing xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error></stream:stream>

quickblox上的app配置似乎也是正确的。我已经创建了一个我在此应用中使用的用户。我还创建了一个公共对话,也在这个应用程序中使用。不确定这里发生了什么。

顺便说一下,我不能使用Quickblox提供的SDK,因为我已经在基于java框架构建的Web服务器上部署了这个代码。

1 个答案:

答案 0 :(得分:0)

不正确的解决错误说明了什么

这个jid很奇怪4461610-26179 @ chat.quickblox.com @ chat.quickblox.com

应该只是4461610-26179@chat.quickblox.com

这就是您收到此错误的原因

我的代码中没有看到这可能是一个问题,无论如何请检查这种可能性