gpt4 book ai didi

Java Smack 4.1 XMPP 可以发送消息但无法接收消息

转载 作者:行者123 更新时间:2023-11-30 07:18:24 28 4
gpt4 key购买 nike

我正在开发一个 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 客户端中),我还编辑了我的代码作为新版本

这是带有精彩截图的证明

SparkClient

但是我仍然无法检索我的消息:我在日志中看到它们,但聊天监听器中的方法未被调用

日志

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

最佳答案

假设:

  • 服务器存在(为什么不安装本地机器 openfire 或其他什么?)
  • 用户“sender”已正确登录此服务器
  • 用户“receiver”已在服务器中正确注册
  • 您可以控制 2 个客户端用户“发送者”和用户“接收者”(为什么不安装为第二个客户端 Spark?),或者“接收者”,它是一个处于回显模式的工作机器人。

您无法在“聊天”中接收外发消息,但可以在群聊中接收。在聊天中,您将能够为“接收者”消息设置监听器。

关于您的代码以及如何修复:

您正在尝试在登录之前和未登录的情况下建立连接并注册聊天监听器。

正确顺序:

  1. 连接
  2. 登录
  3. 新聊天
  4. ChatManager.getInstance(connection).addChatListener

现在,当“接收者”向您发送消息时,您将能够在此聊天中收听传入消息

关于Java Smack 4.1 XMPP 可以发送消息但无法接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38010760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com