首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASmack MultiUserChat ( MUC )无法监听消息

ASmack MultiUserChat ( MUC )无法监听消息
EN

Stack Overflow用户
提问于 2015-02-18 07:11:08
回答 1查看 939关注 0票数 0

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

只要连接到XMPP,登录并创建一个MUC (以及加入一个已经创建的MUC),一切都很顺利。

我只是似乎不能正确地监听发送的消息,我不确定为什么。

我以前使用Javascript ( StropheJS和Bosh )处理过几次XMPP,所以我对它的协议比较熟悉。

因为我已经尝试了以下帖子中的建议(这是我找到的关于这个问题的唯一帖子):

How to properly listen for MultiUserChat in Smack?

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

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

我创建了一个服务,这样我的XMPP连接就可以遍及整个应用程序。因为一旦我的应用程序登录,我就需要连接到XMPP,而MUC房间将只在更低的其他活动中使用。好吧,你明白了。所以,下面是代码:

(如果它太长了,很抱歉,我不是故意让人讨厌,我想要的是提供解决它所需的一切)

代码语言:javascript
复制
//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)的组

代码语言:javascript
复制
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());
    }
}

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

代码语言:javascript
复制
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());
    }
}

所以,我有三个方法,我用来尝试让用户正确地收听进来的消息。

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

另外两个在用户加入GroupChat后被调用:

addMessageListener(muc);和listen2Group();

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

代码语言:javascript
复制
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);
    }

}

下面是我发送消息的方式:

代码语言:javascript
复制
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,当消息被发送时,什么也没有发生!

我将断点放在我向您展示的所有监听程序中,但无论我做什么,它们都不会触发。消息已发送,但永远不会到达任何地方。

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2015-02-18 09:30:32

以供将来参考(因为smack在其当前版本中并没有完整的文档记录)

我找到了听众的问题所在!

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

因为它本质上是一个GroupChat,所以我假设它不需要设置消息的接收者。

修复这个问题所需的一切都在我的sendMessage方法中,多了一行:

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

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

在XEP-0045中,它规定对于群聊,它必须具有目的地址才能正常工作。

“在多用户聊天室内发送的消息是一种特殊类型的"groupchat”,并被发送到聊天室本身( room @service),然后反映给所有占有者。“

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28572998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档