我已經在本地機器上用兩個用戶testuser1和testuser2設置了我的明火(jabber服務器),兩個用戶都使用Spark客戶端進行聊天,沒有任何問題,這很好。使用smack發送和接收消息API
Openfire的IP -192.168.1.65
我想用咂嘴API(3.3.0),用於發送和接收消息。我已經寫了發送端代碼發送消息(與testuser1),並測試與testuser2端收到消息的客戶端(與testuser2)消息,但是當我嘗試使用Java代碼接收發件人消息,我無法接收這些發佈消息。
Sender.java
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;
public class Sender
{
public static void main(String a[]) throws XMPPException, InterruptedException
{
XMPPConnection connection = new XMPPConnection("192.168.1.65");
System.out.println(connection);
connection.connect();
connection.login("testuser1", "test123");
Chat chat = connection.getChatManager().createChat("[email protected]", new MessageListener() {
public void processMessage(Chat chat, Message message) {
// Print out any messages we get back to standard out.
System.out.println("Received message: " + message);
}
});
chat.sendMessage("Howdy test1!");
while (true) {
Thread.sleep(50);
}
}
}
Receiver.java
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;
public class Receiver
{
public static void main(String a[]) throws XMPPException,, InterruptedException
{
XMPPConnection connection = new XMPPConnection("192.168.1.65");
System.out.println(connection);
connection.connect();
connection.login("testuser2", "test123");
Chat chat = connection.getChatManager().createChat("[email protected]", new MessageListener() {
public void processMessage(Chat chat, Message message) {
// Print out any messages we get back to standard out.
System.out.println("Received message: " + message);
}
});
chat.sendMessage("Howdy test2!");
while (true) {
Thread.sleep(50);
}
}
}
請幫助我,並建議如果我下面錯誤的做法。
感謝
解決方法二工作了,非常感謝偉大的解決方案。 – Andy