【发布时间】:2017-06-18 07:12:15
【问题描述】:
我正在尝试设置一个简单的示例,用于使用 Openfire 使用 Smack API 发送和接收消息。我关注了这个tutorial
有人就here 的情况提出了同样的问题,但答案提到了我已经用于接收聊天的代码。许多these examples 也使用相同的方法。
public class Receiver {
public static void main(String a[]) throws XMPPException, InterruptedException {
XMPPConnection connection = new XMPPConnection("192.168.1.38");
System.out.println(connection);
connection.connect();
connection.login("test2", "123456");
EFLogger.LogInfo("Receiver", "Connected [ " + connection.isConnected() + " ]");
connection.getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat, boolean b) {
System.out.println("In Message Listener ! ");
chat.addMessageListener(new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Message [ " + message.toXML());
}
});
try {
chat.sendMessage("Hello");
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
while (true) {
}
}
}
为了发送,我正在使用
public class Main {
public static void main(String[] args) throws Exception {
String username = "test";
String password = "123456";
XmppManager xmppManager = new XmppManager("192.168.1.38", 5222);
xmppManager.init();
xmppManager.performLogin(username, password);
xmppManager.setStatus(true, "Hello everyone");
//As @MrPk suggested I shouldn't use "/Smack"
//String buddyJID = "test2@ef-app2/Smack";
String buddyJID = "test2@ef-app2";
String buddyName = "test";
xmppManager.createEntry(buddyJID, buddyName);
for (int i = 0; i < 10; i++) {
xmppManager.sendMessage("Hello mate from test " + i, "test2");
}
boolean isRunning = true;
while (isRunning) {
Thread.sleep(50);
}
xmppManager.destroy();
}
}
这个类是 XMPPManager
/**
This class is responsible for handling all Actions related to Chat Management. Connection, Login, Status, Create Entry and Message Listener.
*/
public class XmppManager {
private static final int packetReplyTimeout = 500; // millis
private String server;
private int port;
private ConnectionConfiguration config;
private XMPPConnection connection;
private ChatManager chatManager;
private MessageListener messageListener;
public XmppManager(String server, int port) {
this.server = server;
this.port = port;
}
public void init() throws XMPPException {
EFLogger.LogInfo("XmppManager", String.format("Initializing connection to server %1$s port %2$d", server, port));
SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
// SmackConfiguration.DEBUG = true;
config = new ConnectionConfiguration(server, port);
config.isDebuggerEnabled();
connection = new XMPPConnection(config);
connection.connect();
EFLogger.LogInfo("XmppManager", "Connected: " + connection.isConnected());
chatManager = connection.getChatManager();
//messageListener = new MyMessageListener();
}
public void performLogin(String username, String password) throws XMPPException {
if (connection != null && connection.isConnected()) {
EFLogger.LogInfo("XmppManager", "Before login userName [ " + username + " ] password [ " + password + " ]");
connection.login(username, password);
System.out.printf("Logged in ");
}
}
public void setStatus(boolean available, String status) {
Presence.Type type = available ? Type.available : Type.unavailable;
Presence presence = new Presence(type);
presence.setStatus(status);
connection.sendPacket(presence);
}
public void destroy() {
if (connection != null && connection.isConnected()) {
connection.disconnect();
}
}
public void sendMessage(String message, String buddyJID) throws XMPPException {
EFLogger.LogInfo("XmppManager", String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
Chat chat = chatManager.createChat(buddyJID, messageListener);
chat.sendMessage(message);
}
public void createEntry(String user, String name) throws Exception {
EFLogger.LogInfo("XmppManager", String.format("Creating entry for buddy '%1$s' with name %2$s", user, name));
Roster roster = connection.getRoster();
roster.createEntry(user, name, null);
}
static class MyMessageListener implements MessageListener {
public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
EFLogger.LogInfo("XmppManager", String.format("Received message '%1$s' from %2$s", message.getError(), from));
}
}
}
首先我认为buddyID肯定有问题,我有这两个用户test和test2,XMPPdomain是ef-app2所以我设置了buddyJID
test2@ef-app2/Smack smack 是默认资源。
编辑
@MrPk 建议不要使用 /Smack ,但没有运气。
但是,它仍然没有影响我仍然无法收到任何消息。 我不确定我错过了什么。 如果您有兴趣重现相同的问题,可以找到 IntelliJ IDEA 项目here 您可以获取有关该问题的更多详细信息here
救命!
【问题讨论】:
-
发送消息时删除资源:发送到“test2@ef-app2”而不是“test2@ef-app2/Smack”。 Packet Timeout 真的很短,默认是3000,测试的时候增加到5000!代码的第一部分是教程?如果是,我建议删除它,这是误导
-
感谢@MrPk 的回复,我试过删除smack,但没有用。第一个只是显示我在做什么的示例,我用我正在使用的原始代码对其进行了更新,并且它使用了默认的数据包超时。
-
现在很清楚了。当我可以时,我会更好地看一下并回复答案;)