无法使用 smack 接收群组消息

Posted

技术标签:

【中文标题】无法使用 smack 接收群组消息【英文标题】:Not able to receive group message using smack 【发布时间】:2017-04-21 11:37:03 【问题描述】:
 var multiUserManager: MultiUserChatManager = MultiUserChatManager.getInstanceFor(constants.obj.connection)
        var multiUserChat: MultiUserChat =multiUserManager.getMultiUserChat(JidCreate.entityBareFrom(roomName))

         var a: Resourcepart = Resourcepart.from(PreferenceManager.getDefaultSharedPreferences(applicationContext).getString("j_id", null))
         multiUserChat.join(a)

       multiUserChat.addMessageListener  object: MessageListener 
           override fun processMessage(message: Message?) 
               Log.v("Message is ",message.toString())
               Log.v("Message from ",message!!.from.toString())
               Log.v("Message is ",message.getBody())
               Log.v("Message subject ",message.getSubject())
               Log.v("Message is ",message.body)
           

        

我也试过这个。

 multiUserChat.addMessageListener  GroupChatListener() 

 class GroupChatListener : StanzaListener 
    override fun processStanza(packet: Stanza?) 

        Log.v("Packet is ",packet.toString())
        Log.v("From",packet!!.from.toString())
        Log.v("Stanza id ",packet.stanzaId)
    

当有人在服务器上发送消息时,我无法接收消息,但我能够发送消息。

编辑:

这是我按下加入按钮时得到的输出。我在 logcat 中没有收到任何错误。

 04-25 22:31:45.460 13376-13376/com.example.itstym.smackchat D/on click: fbjg
04-25 22:31:45.460 13376-13376/com.example.itstym.smackchat D/Room name: room1
04-25 22:31:45.460 13376-13376/com.example.itstym.smackchat D/Nick name: ank
04-25 22:31:45.479 13376-13376/com.example.itstym.smackchat V/Multiuser chat: MUC: room1@conference.replica3377.cloudapp.net(user1@replica3377.cloudapp.net/1540794269789260263741678)
04-25 22:31:45.481 13376-16060/com.example.itstym.smackchat D/SMACK: SENT (0): <iq to='conference.replica3377.cloudapp.net' id='X2GPV-38' type='get'><query xmlns='http://jabber.org/protocol/disco#info'></query></iq>
04-25 22:31:45.482 13376-16060/com.example.itstym.smackchat D/SMACK: SENT (0): <r xmlns='urn:xmpp:sm:3'/>
04-25 22:31:47.538 13376-16061/com.example.itstym.smackchat D/SMACK: RECV (0): <a h='15' xmlns='urn:xmpp:sm:3'/><iq xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='conference.replica3377.cloudapp.net' type='result' id='X2GPV-38'><query xmlns='http://jabber.org/protocol/disco#info'><identity name='Chatrooms' type='text' category='conference'/><feature var='http://jabber.org/protocol/disco#info'/><feature var='http://jabber.org/protocol/disco#items'/><feature var='jabber:iq:register'/><feature var='http://jabber.org/protocol/muc'/><feature var='vcard-temp'/><feature var='urn:xmpp:mucsub:0'/><feature var='http://jabber.org/protocol/muc#unique'/><feature var='http://jabber.org/protocol/rsm'/><feature var='urn:xmpp:mam:tmp'/><feature var='urn:xmpp:mam:0'/><feature var='urn:xmpp:mam:1'/><x type='result' xmlns='jabber:x:data'><field var='FORM_TYPE' type='hidden'><value>http://jabber.org/network/serverinfo</value></field></x></query></iq><r xmlns='urn:xmpp:sm:3'/>
04-25 22:31:47.541 13376-16060/com.example.itstym.smackchat D/SMACK: SENT (0): <a xmlns='urn:xmpp:sm:3' h='16'/><presence to='room1@conference.replica3377.cloudapp.net/ank' id='X2GPV-37'><x xmlns='http://jabber.org/protocol/muc'></x><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='os2Kusj3WEOivn5n4iFr/ZEO8ls='/></presence>
04-25 22:31:47.823 13376-16061/com.example.itstym.smackchat D/SMACK: RECV (0): <presence xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net/an' id='Kd5p0-48'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='os2Kusj3WEOivn5n4iFr/ZEO8ls='/><x xmlns='http://jabber.org/protocol/muc#user'><item role='participant' affiliation='none'/></x></presence>
04-25 22:31:47.835 13376-16061/com.example.itstym.smackchat D/SMACK: RECV (0): <r xmlns='urn:xmpp:sm:3'/><presence xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net/ank' id='X2GPV-37'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='os2Kusj3WEOivn5n4iFr/ZEO8ls='/><x xmlns='http://jabber.org/protocol/muc#user'><item jid='user1@replica3377.cloudapp.net/1540794269789260263741678' role='participant' affiliation='none'/><status code='110'/></x></presence><message xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net/user1' type='groupchat' id='54f75761-b5cd-4955-8d32-5609e89a2417'><x xmlns='jabber:x:event'><composing/></x><delay from='room1@conference.replica3377.cloudapp.net' stamp='2017-04-21T11:52:40.966514Z' xmlns='urn:xmpp:delay'/><body>hey</body></message><message xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net/user1' type='groupchat' id='1492776073223:msg'><active xmlns='http://jabber.org/protocol/chatstates'/><delay from='room1@conference.replica3377.cloudapp.net' stamp='2017-04-21T12:01:13.896472Z' xmlns='urn:xmpp:delay'/><body>asd&apos;</body></message>
04-25 22:31:47.835 13376-16060/com.example.itstym.smackchat D/SMACK: SENT (0): <a xmlns='urn:xmpp:sm:3' h='17'/>
04-25 22:31:47.836 13376-13376/com.example.itstym.smackchat V/Joined response: kotlin.Unit
04-25 22:31:47.836 13376-13376/com.example.itstym.smackchat V/Joined: true
04-25 22:31:47.836 13376-13376/com.example.itstym.smackchat I/Choreographer: Skipped 142 frames!  The application may be doing too much work on its main thread.
04-25 22:31:47.841 13376-16061/com.example.itstym.smackchat D/SMACK: RECV (0): <message xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net/ankur' type='groupchat' id='05333f72-0ffc-49ba-8180-23c3b00b5d31'><x xmlns='jabber:x:event'><composing/></x><delay from='room1@conference.replica3377.cloudapp.net' stamp='2017-04-24T11:02:35.386083Z' xmlns='urn:xmpp:delay'/><body>jfdgsdk</body></message><message xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net/wefa' type='groupchat' id='e0e5cf9a-a363-4f05-a8cf-b2d1016a99d2'><x xmlns='jabber:x:event'><composing/></x><delay from='room1@conference.replica3377.cloudapp.net' stamp='2017-04-24T11:25:57.859462Z' xmlns='urn:xmpp:delay'/><body>dgdfgsdfgsg</body></message><message xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net/abv' type='groupchat' id='fr3u8-51'><delay from='room1@conference.replica3377.cloudapp.net' stamp='2017-04-24T11:28:05.832722Z' xmlns='urn:xmpp:delay'/><body>hi</body></message><message xml:lang='en' to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net/wefa' type='groupchat' id='6259ccab-5235-4367-8b57-f03f27ec8c9a'><x xmlns='jabber:x:event'><composing/></x><delay from='room1@conference.replica3377
04-25 22:31:47.873 13376-16061/com.example.itstym.smackchat D/SMACK: RECV (0): .cloudapp.net' stamp='2017-04-24T11:28:24.801581Z' xmlns='urn:xmpp:delay'/><body>welcome abv</body></message><message to='user1@replica3377.cloudapp.net/1540794269789260263741678' from='room1@conference.replica3377.cloudapp.net' type='groupchat'><subject/></message>
04-25 22:31:47.881 13376-16096/com.example.itstym.smackchat W/MultiUserChat: Message subject not changed by a full JID: room1@conference.replica3377.cloudapp.net
04-25 22:31:48.030 13376-16061/com.example.itstym.smackchat D/SMACK: RECV (0): <r xmlns='urn:xmpp:sm:3'/>
04-25 22:31:48.031 13376-16060/com.example.itstym.smackchat D/SMACK: SENT (0): <a xmlns='urn:xmpp:sm:3' h='25'/>

编辑 2:

multiUserChat?.addMessageListener 
                    Log.v("listener","yup")
                    MessageListener  message ->
                        Log.v("Message is complete ",message.toString())
                        Log.v("Message from ",message?.from.toString())
                        Log.v("Message is body",message?.getBody())
                        Log.v("Message subject ",message?.getSubject())
                        Log.v("Message body ",message?.body)
                    
                

每当有人向我发送 Log.v("listener","yup") 调用的消息时。但是不要进入messagelistener。 我已阅读日志输出并找到它们,我正在以 xml 的形式获取 msg。

【问题讨论】:

每次登录成功后是否都尝试加入房间。 是的。登录后我调用 multiUserChat.join(a) @Malik 加入会有什么反应? @Farhan 加入这里的方式是什么?你能澄清一下吗? @Farhan multiUserChat.join(a) 返回 kotlin.unit 【参考方案1】:

我做了什么:

首先加入 GroupChat,然后创建一个自定义侦听器以获取错过的消息,并在 onSuccess 上添加 messageListener。那就是向我提供 List 中的历史消息,然后应用开始收听来自此 GroupChat 的新消息。

public void joinToGroupChat(final MultiUserChat multiUserChat, final OpenFireListener<List<Message>> listener) 

    if (!multiUserChat.isJoined()) 
       multiUserChat.join(connection.getUser().getResourcepart());
    

    getPendingMessages(multiUserChat, new OpenFireListener<List<Message>>() 
        @Override
        public void onSuccess(List<Message> result) 
            multiUserChat.addMessageListener(messageListener);
            listener.onSuccess(result);
        

        @Override
        public void onError(String message) 
            listener.onError(message);
        
    );


private MessageListener messageListener = new MessageListener() 
    @Override
    public void processMessage(Message message) 
        //I only use the subject and body in my project
        listener.notifyMessage(message.getSubject(), message.getBody());
    
;

确保您在相同的上下文中加入了 GroupChat 并实现了您的自定义侦听器。这是我的通用实现。

public interface OpenFireListener<T>
    void onSuccess(T result);
    void onError(String message);


public interface OpenFireServerListener 
    enum STATE ERROR, CONNECTION_CLOSED, RECONNECTION_SUCCESS, RECONNECTION_FAILED, AUTHENTICATED, NOT_AUTHORIZED, CONNECTED

    void notifyStatusOpenFireServer(STATE state, String message);
    void notifyMessage(String streamName, String streamId);


【讨论】:

以上是关于无法使用 smack 接收群组消息的主要内容,如果未能解决你的问题,请参考以下文章

我无法在 smack android 的 MessageListener 方法中接收离线消息

SmackInitialization:无法确定 Smack 版本

无法在 JAVA 中使用 SMACK api 显示收到的消息

无法使用 smackx MultiUserChat API 接收聊天消息

无法使用 Smack 创建 MultiUserChat:接收 SmackException$NoResponseException:在回复中未收到响应

使用 smack 的多用户聊天没有收到消息侦听器