我是Android新手,我嘗試使用smack api和ejabberd創建聊天應用程序。 我實現一對一聊天,其工作很好。但我無法收到聊天室消息。 我該怎麼辦?如何使用samck API接收聊天室消息,XMPP
這裏是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 sendGroupMessage所有連接XMPP連接()是用來發送聊天室的消息,並且其工作良好。 問題是收到聊天室消息。
意味着您可以發送消息,而不是接受他們在1附加傳入消息監聽器:1聊天? –
是@SarthakMittal –
好的,那麼您如何驗證郵件已成功發送? –