我正在开发一个Java应用程序来与XMPP中的设备聊天。
根据日志,我能够发送消息,但我无法接收消息
这是我的代码
XMPPListener.java
package messaging;
import java.io.IOException;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
public class XMPPListener implements MessageListener {
AbstractXMPPConnection connection;
public XMPPListener(String host, int port, String serviceName){
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder().
setHost(host)
.setServiceName(serviceName)
.setSecurityMode(SecurityMode.ifpossible)
.setPort(port)
.setDebuggerEnabled(true)
.build();
this.connection = new XMPPTCPConnection(config);
}
public void connect() {
try {
this.connection.connect();
} catch (SmackException | IOException | XMPPException e) {
e.printStackTrace();
}
}
public void login(String userName, String password) throws XMPPException {
try {
this.connection.login(userName, password);
} catch (SmackException | IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String message, String to) throws XMPPException {
Chat chat = ChatManager.getInstanceFor(connection).createChat(to);
try {
chat.sendMessage(message);
} catch (NotConnectedException e) {
e.printStackTrace();
}
}
public void listenChat(String name){
ChatManager manager = ChatManager.getInstanceFor(this.connection);
manager.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
System.out.println("Created chat");
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
System.out.println(message.getBody());
}
});
}
});
}
public void disconnect() {
connection.disconnect();
}
@Override
public void processMessage(Message message) {
System.out.println("Received something: " + message.getBody());
}
}
Main.java
XMPPListener xmppListener = new XMPPListener("ajabber.me",5222,"ajabber.me");
try {
xmppListener.connect();
xmppListener.login(user, password);
xmppListener.listenChat(to);
while(true){
xmppListener.sendMessage("Spack me spack me, Ho !", to);
Thread.sleep(10000);
}
} catch (XMPPException e2) {
e2.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在此代码中,我连接到我在此处找到的随机服务器:list of servers。然后我尝试发送一些消息 我有以下日志
11:51:56 AM SENT (0): <stream:stream xmlns='jabber:client' to='ajabber.me' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
11:51:56 AM RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='11102383658626487319' from='ajabber.me' version='1.0' xml:lang='en'><stream:features><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-
one.net/en/ejabberd/' ver='9BNWaDsRr/HNe8AdlF+JvcDY2L0='/><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls></stream:features>
11:51:56 AM SENT (0): <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>
11:51:56 AM RECV (0): <proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
11:51:57 AM SENT (0): <stream:stream xmlns='jabber:client' to='ajabber.me' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
11:51:57 AM RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='9208753033211689749' from='ajabber.me' version='1.0' xml:lang='en'>
11:51:57 AM RECV (0): <stream:features><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='9BNWaDsRr/HNe8AdlF+JvcDY2L0='/><register xmlns='http://jabber.org/features/iq-register'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism><mechanism>DIGEST-MD5</mechanism><mechanism>X-OAUTH2</mechanism><mechanism>SCRAM-SHA-1</mechanism></mechanisms></stream:features>
chat created
11:51:57 AM SENT (0): <message to='test@ajabber.me' id='9ptjB-3' type='chat'><body>coucou</body><thread>d0b9f5a3-515a-4d53-9795-ca112962d4f8</thread></message>
chat created
11:51:59 AM SENT (0): <message to='test@ajabber.me' id='9ptjB-4' type='chat'><body>coucou</body><thread>a6dbc7a0-2b3a-4a30-9be6-2b1e2995cadb</thread></message>
鉴于这些日志,我猜消息已发送,聊天也已创建,但似乎我从未收到过消息。你能帮我解决这个问题吗?
我运行java 1.8和Smack 4.7
修改
我尝试过使用Spark:我设法与我的应用程序交谈(在Spark客户端中),我也将我的代码编辑为新版本
这是带有精彩截图的证明
但是我仍然无法检索我的消息:我在日志中看到它们,但聊天监听器中的方法没有被调用
日志
03:17:22 PM RECV (0): <message from='alexi@ajabber.me/Spark' to='java@ajabber.me/Smack' id='NdNHr-150' type='chat'><body>Wololo</body><thread>5292f083-bba9-4a63-ab40-9718b5e00bd0</thread><x xmlns='jabber:x:event'><offline/><composing/></x></message>
最后编辑
我的代码正在使用上面的代码,特别感谢MrKp
答案 0 :(得分:1)
假设:
您无法在&#34;聊天&#34;中收到外发留言,但您可以在群聊中进行操作。在聊天中,您将能够为&#34; reciver&#34;设置听众。消息。
关于您的代码以及如何修复:
您正在尝试进行连接并在登录前注册聊天的监听器,而无需登录。
正确的顺序:
现在,当&#34; reciver&#34;时,您将能够在此聊天中收听传入的消息。会发给你一个