【问题标题】:MultiUserChat Send and Receive Message errorMultiUserChat 发送和接收消息错误
【发布时间】:2018-10-15 08:37:32
【问题描述】:

我正在开发 android 聊天应用程序(xmpp 服务器 -prosody- 和 android smack 库) 我成功创建了群组房间并邀请了成员,但是当我尝试向群组发送消息时出现此节错误:

<message to='rokayah89@eonaws.com/Roo' from='room31@conference.eonaws.com' id='123' type='error'><error type='cancel'><not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></message>

我的发送消息代码:

    MultiUserChat muc = manager.getMultiUserChat(roomBarJid);



    Message msg = new Message(roomBarJid);

    msg.setType(Message.Type.groupchat);
    msg.setBody("Hi there");
    msg.setStanzaId("123");
    msg.setSubject("Rokayah ..... ");
    msg.setTo(roomBarJid);

    try {
      if (muc != null) {
          muc.sendMessage(msg);
      }       Log.d("GROUP", "The message send..............");
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

这是接收消息的监听器:

    StanzaFilter filter = new StanzaTypeFilter(Message.class);
    mConnection.addSyncStanzaListener(new StanzaListener() {
        @Override
        public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException, SmackException.NotLoggedInException {

       Message message = (Message) packet;
       String body = message.getBody();

       Log.d("GROUP" , "here :" +body);



        }
    }, filter);

我不知道发送和接收监听器给我空消息正文有什么问题。

任何帮助请!!

【问题讨论】:

  • 我假设你正在使用 XMPP MUC;根据 MUC 的实现,这个错误意味着你不是群组的成员,所以可能有两个原因:1.你还没有加入房间 2.你不允许发送消息 请解释一下你在做什么问题的确切说明。

标签: android xmpp chat smack


【解决方案1】:

您必须先加入房间,然后才能发送 XMPP 消息。

要加入 xmpp 房间,您必须发送如下所示的存在节:

<presence
    from='hag66@shakespeare.lit/pda'
    id='n13mt3l'
    to='coven@chat.shakespeare.lit/thirdwitch'>
  <x xmlns='http://jabber.org/protocol/muc'/>
</presence>

在 java 中是这样的:

Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(mThreadId);
joinPresence.addExtension(new MUCInitialPresence());

XMPPConnection conx = Application.getInstance().getXMPPConection();
PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(mThreadId), new PacketTypeFilter(Presence.class));

PacketCollector response = conx.createPacketCollector(responseFilter);
conx.sendPacket(joinPresence);

Presence presence = (Presence) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
response.cancel();

if (presence == null) {
    Log.e("XMPP", "No response from server.");
} else if (presence.getError() != null) {
    Log.e("XMPP", presence.getError().toString());
}

【讨论】:

    【解决方案2】:

    首先,您需要加入一个房间并确保其他群组用户也加入该群组,为此您必须发送如下的群组加入邀请。

    public static void inviteToGroup(String inviteuser, String groupName) {
    
        if (TextUtils.isEmpty(inviteuser) || TextUtils.isEmpty(groupName)) return;
    
        try {
    
            EntityBareJid mucJid = JidCreate.entityBareFrom(groupName + "@" + Constants.GRP_SERVICE);
    
            Resourcepart nickname = Resourcepart.from(userId);
    
            mucChatManager = MultiUserChatManager.getInstanceFor(MyApplication.connection);
            mucChat = mucChatManager.getMultiUserChat(mucJid);
            Message message = new Message();
            // message.setType(Type.normal);
            message.setSubject(Constants.GROUP_CHAT_MSG_MODE);
            message.setBody(Constants.GROUP_GREETINGS);
            EntityBareJid eJId = JidCreate.entityBareFrom(inviteuser + "@" + Constants.XMPP_DOMAIN);
    
    
    
            /*MucEnterConfiguration.Builder mucEnterConfiguration
                    = mucChat.getEnterConfigurationBuilder(nickname).requestHistorySince(sinceDate);*/
    
            MucEnterConfiguration.Builder mucEnterConfiguration
                    = mucChat.getEnterConfigurationBuilder(nickname).requestNoHistory();
    
            mucChat.join(mucEnterConfiguration.build());
    
            LogM.e("Room joined");
    
            //  mucChat.invite(message, eJId, groupName);
    
        } catch (XmppStringprepException e) {
            e.printStackTrace();
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (SmackException.NoResponseException e) {
            e.printStackTrace();
        } catch (XMPPException.XMPPErrorException e) {
            e.printStackTrace();
        } catch (MultiUserChatException.NotAMucServiceException e) {
            e.printStackTrace();
        }
    
    
    
    }
    

    这是我发送群组消息的代码,您需要将消息类型添加为Type.groupchat

    public boolean sendGrpMessage(ChatPojo chatPojo, String grp_name) {
        try {
    
            final String body = gson.toJson(chatPojo);
    
            Message msg = new Message();
            msg.setType(Type.groupchat);
            msg.setSubject("chat");
            msg.setBody(body);
    
            EntityBareJid mucJid = JidCreate.entityBareFrom(grp_name + "@" + Constants.GRP_SERVICE);
            mucChatManager = MultiUserChatManager.getInstanceFor(MyApplication.connection);
            mucChat = mucChatManager.getMultiUserChat(mucJid);
    
            mucChat.sendMessage(msg);
            //DataManager.getInstance().updateReceiptReceived(msgReceipt,Constants.MESSAGE_STATUS_NOT_DELIVERED);
            return true;
    
        } catch (XmppStringprepException | InterruptedException | SmackException.NotConnectedException e) {
            Log.d(TAG, "sendGrpMessage() Error = [" + e.getMessage() + "]");
            return false;
        }
    
    }
    

    之后添加群组消息监听器

    StanzaFilter filter = MessageTypeFilter.GROUPCHAT;
        MyApplication.connection.addAsyncStanzaListener(new StanzaListener() {
            @Override
            public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
    
    
    
                Message message = (Message) packet;
    
                if (message.getType() == Type.groupchat && message.getBody() != null) {
    
                    LogM.e("+++++++++++++++++++++++++++GROUPCHAT+++++++++++++++++++++++++++++++++");
                    LogM.e("from: " + message.getFrom());
                    LogM.e("xml: " + message.getType().toString());
                    LogM.e("Got text [" + message.getBody() + "] from [" + message.getFrom() + "]");
    
    
                    LogM.e("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    
    
                    } else if (message.getType() == Type.error) {
    
                    Toast.makeText(service, "error type", Toast.LENGTH_SHORT).show();
    
                }  
    
    
            }
        }, filter);
    

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多