0

我有这个 Android 应用程序,我一直在尝试设置 XMPP MultiUserChat。

至于连接到 XMPP,登录并创建一个 MUC(以及加入一个已经创建的)一切都很好。

我似乎无法正确收听发送的消息,我不知道为什么。

我之前曾使用过几次使用 Javascript 的 XMPP(带有 Bosh 的 StropheJS),所以我对它的协议有点熟悉。

正如我已经尝试过以下帖子中的建议(我发现的关于此事的唯一帖子):

如何在 Smack 中正确收听 MultiUserChat?

所以,我决定问你一些方向,因为我认为这可能只是我一直错过的一个设置。

所以,为了简单起见,我将提供我的整个序列。

我创建了一个服务,因此我的 XMPP 连接可以在整个应用程序中普遍存在。因为我需要在我的应用程序登录后立即连接到 XMPP,并且 MUC 房间只会在其他活动中进一步使用。好吧,你明白了。所以,这里是代码:

(对不起,如果它有点太长了,我并不是要烦人,我只想提供解决问题所需的一切)

//everything is alright here.
//it's connecting correctly to XMPP
public void connect()
{

    Thread t = new Thread(new Runnable() {

        @Override
        public void run()
        {
            boolean flConnected = false;
            System.setProperty("smack.debugEnabled", "true");
            SmackConfiguration.setDefaultPacketReplyTimeout(10000);


            ConnectionConfiguration ConnectionConfiguration = new ConnectionConfiguration(Host, Port);
            ConnectionConfiguration.setSecurityMode(org.jivesoftware.smack.ConnectionConfiguration.SecurityMode.disabled);

            ConnectionConfiguration.setDebuggerEnabled(true);
            ConnectionConfiguration.setSendPresence(true);

            connection = new XMPPTCPConnection(ConnectionConfiguration);

            try
            {
                connection.connect();
                login();
            }
            //there are many exceptions being treated, I 
            //suppressed them here for simplicity's sake
            catch (Exception e)
            {
                Log.e(TAG, "connection exception: " + e.getMessage());
            }


        }
    });
    t.start();
}

public void login()
{
    try
    {
        connection.login(this.User, Pwd);

        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);
        this.setLoginAttempts();           

    }
    catch (Exception e)
    {
        Log.e(TAG, "connection exception: "+e.getMessage());
    }
}

因此,当用户应该是某个聊天组的管理员时,我创建传递 GroupName (id) 的组

public void createGroup(String strRoom)
{
    try
    {
        muc = new MultiUserChat(connection, strRoom+"@"+this.GroupChat);
        muc.create(this.User);

        Form form = muc.getConfigurationForm();
        Form submitForm = form.createAnswerForm();



        for (Iterator fields = form.getFields().iterator(); fields.hasNext();) {
            FormField field = (FormField) fields.next();
            if (!FormField.TYPE_HIDDEN.equals(field.getType())
                    && field.getVariable() != null) {
                    submitForm.setDefaultAnswer(field.getVariable());
            }
        }

        List<String> owners = new ArrayList<>();
        owners.add(this.User + "@"+this.Host);

        muc.sendConfigurationForm(submitForm);                        

        //here, it's another FAILED attempt of putting the 
        //listener to an async class. Didn't work at all!!
        //just kept it here so you can see my attempts!
        //new MessageRunner().execute(connection);           

    }
    catch (Exception ex)
    {
        Log.e(TAG, " exception :"+ex.getMessage());
    }
}

当它只是一个参与者时,它会加入该组:

public void joinGroup(String strRoom)
{
    try
    {
        DiscussionHistory history = new DiscussionHistory();
        history.setMaxStanzas(50);
        muc = new MultiUserChat(connection, strRoom+"@"+this.GroupChat);
        muc.join(strRoom, this.Pwd, history, connection.getPacketReplyTimeout());

        this.addMessageListener(muc);

        this.listen2Group();
    }
    catch (Exception ex)
    {
        Log.e(TAG, "Exception :"+ex.getMessage());
    }
}

因此,我使用了 3 种方法来尝试让用户正确收听传入的消息。

其中之一是在登录后立即设置: setListener(connection);

其他两个在用户加入群聊后被调用:

addMessageListener(muc); 和listen2Group();

我把它们都留在这里,这样你就可以看到我走了多远:

private void addMessageListener(MultiUserChat muc)
{
    //it's never coming here! never ever!!!
    if(null != muc){
        muc.addMessageListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) {
                Log.i("processPacket", "receiving message");
            }
        });
    }
}

private void listen2Group()
{

    //also, NEVER EVER getting here at any time!!
    PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);


    connection.addPacketListener(new PacketListener() {
        @Override
        public void processPacket(Packet packet) throws NotConnectedException
        {
            Message message = (Message) packet;
            if (message.getBody() != null)
            {

                //message should be treated here, 
                //but I suppressed it as well.
            }
        }
    }, filter);
}

public void setListener(XMPPConnection cnx)
{
    this.connection = cnx;
    if (connection != null) {

        PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
        connection.addPacketListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) 
                {                        
                   //message will be treated here.
                }
            }
        }, filter);
    }

}

这是我发送消息的方式:

public void sendMessage(String msgText)
{
    try
    {

        Message msg = new Message();
        msg.setBody(msgText);
        msg.setType(Message.Type.groupchat);


        muc.sendMessage(msg);

        //I tried sending with this one as well, nope!
        //connection.sendPacket(msg);
    }
    catch(Exception ex)
    {
        Log.e(TAG, " exception :"+ex.getMessage());
    }
}

所以,可能你已经看到我做错了。但我已经为此工作了好几天。什么都没有通过!

当我检查我的服务器( openfire )时,所有用户都被设置为在线,并且正在正确登录到 MUC,当消息发送时,没有任何反应!

我在我展示给你的所有侦听器上都设置了断点,但无论我做什么,它们都不会触发。消息已发送,但它永远不会到达任何地方。

有任何想法吗?

4

1 回答 1

0

供将来参考(因为当前版本中的 smack 并没有全部记录)

我发现听众有什么问题!

事实证明,我没有将目的地设置为聊天组。

由于它本质上是 GroupChat,我认为它不需要将接收者设置为消息。

一切都需要解决这个问题,在我的 sendMessage 方法中,一个额外的行:

msg.setTo(this.room+"@"+my_service_address);

http://xmpp.org/extensions/xep-0045.html

在 XEP-0045 中,它声明对于群聊,它必须有一个目标地址才能正常工作。

“在多用户聊天室中发送的消息属于特殊类型的“群聊”,并发送给房间本身(room@service),然后反映给所有居住者。”

于 2015-02-18T01:30:31.923 回答