如何使用 samck API、XMPP 接收聊天室消息
Posted
技术标签:
【中文标题】如何使用 samck API、XMPP 接收聊天室消息【英文标题】:How to receive chatroom message using samck API , XMPP 【发布时间】:2017-08-08 10:34:02 【问题描述】:我是 android 新手,我尝试使用 smack api 和 ejabberd 创建聊天应用程序。 我实现了一对一的聊天,效果很好。但我无法接收聊天室消息。 我该怎么办?
这里是 xmpp.java
public class MyXMPP
public MyXMPP(MyService context, String serverAdress, String logiUser,
String passwordser)
this.serverAddress = serverAdress;
this.loginUser = logiUser;
this.passwordUser = passwordser;
this.context = context;
this.applicationContext = context;
init();
public static MyXMPP getInstance(MyService context, String server,String user, String pass)
if (instance == null)
instance = new MyXMPP(context, server, user, pass);
instanceCreated = true;
return instance;
public org.jivesoftware.smack.chat.Chat Mychat;
ChatManagerListenerImpl mChatManagerListener;
MMessageListener mMessageListener;
static
try
Class.forName("org.jivesoftware.smack.ReconnectionManager");
catch (ClassNotFoundException ex)
// problem loading reconnection manager
public void init()
gson = new Gson();
mMessageListener = new MMessageListener(context);
mChatManagerListener = new ChatManagerListenerImpl();
initialiseConnection();
private void initialiseConnection()
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setServiceName(serverAddress);
config.setHost("192.168.0.101");
config.setPort(5222);
config.setDebuggerEnabled(true);
XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);
connection = new XMPPTCPConnection(config.build());
XMPPConnectionListener connectionListener = new XMPPConnectionListener();
connection.addConnectionListener(connectionListener);
private class ChatManagerListenerImpl implements ChatManagerListener
@Override
public void chatCreated(final org.jivesoftware.smack.chat.Chat chat,
final boolean createdLocally)
if (!createdLocally)
chat.addMessageListener(mMessageListener);
public void sendMessage(ChatMessage chatMessage)
String body = gson.toJson(chatMessage);
Mychat = ChatManager.getInstanceFor(connection).createChat(
chatMessage.receiver + "@"
+ context.getString(R.string.server),
mMessageListener);
chat_created = true;
final Message message = new Message();
message.setBody(body);
message.setStanzaId(chatMessage.msgid);
message.setTo(chatMessage.receiver);
message.setFrom(chatMessage.sender);
message.setType(Message.Type.chat);
Mychat.sendMessage(chatMessage.body);
public void sendGroupMessage(ChatMessage chatMessage)
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
MultiUserChat multiUserChat = manager.getMultiUserChat(chatMessage.receiver);
try
multiUserChat.join(chatMessage.sender,"12345");
// multiUserChat.addMessageListener((MessageListener) mMessageListener);
catch (XMPPException.XMPPErrorException e)
e.printStackTrace();
catch (SmackException e)
e.printStackTrace();
Message message = new Message(chatMessage.receiver, Message.Type.groupchat);
message.setBody(chatMessage.body);
message.setType(Message.Type.groupchat);
message.setFrom(chatMessage.sender);
message.setTo(chatMessage.receiver);
multiUserChat.sendMessage(message);
/* Its work good for one to one chat */
private class MMessageListener implements ChatMessageListener
Context context;
public MMessageListener(Context c)
this.context = c;
@Override
public void processMessage(final org.jivesoftware.smack.chat.Chat chat,
final Message message)
if (message.getType() == Message.Type.chat
&& message.getBody() != null)
Log.d("Message received",message.getBody);
我在xmpp.java中实现了所有的xmpp连接 sendGroupMessage() 用于发送聊天室消息,效果很好。 问题是接收聊天室消息。
我也收到消息到 android 日志 这是图片
【问题讨论】:
表示您可以在 1:1 聊天中发送消息但无法接收消息? 是的@SarthakMittal 好的,那么您如何验证消息是否已成功发送? Smack 将返回已发送消息的状态,如已发送或错误 【参考方案1】:您使用的Chat
类已弃用!
我建议您将 smack 库更新到最新版本(目前为 4.2)。
这是在 Smack-4.2
中附加传入消息侦听器的方法XMPPTCPConnection connection; // Assuming you have the connection object
org.jivesoftware.smack.chat2.ChatManager chatManager = org.jivesoftware.smack.chat2.ChatManager.getInstanceFor(connection);
chatManager.addIncomingListener(new IncomingChatMessageListener()
@Override
public void newIncomingMessage(EntityBareJid from, Message message, org.jivesoftware.smack.chat2.Chat chat)
// Message received here
);
【讨论】:
你能只发布相关部分吗:) 有空我会看看:)以上是关于如何使用 samck API、XMPP 接收聊天室消息的主要内容,如果未能解决你的问题,请参考以下文章