Java 如何使用smack库构建客户端应用程序,以便在传入消息不断到来时接收它们?

Java 如何使用smack库构建客户端应用程序,以便在传入消息不断到来时接收它们?,java,xmpp,smack,Java,Xmpp,Smack,我在使用smack库连接到Google服务器时使用了此代码。但问题是我不知道如何保持代码运行,以便我可以继续发送和接收消息。请帮助我。我正在复制粘贴代码: // connect to gtalk server ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com"); XMPPConnection connection = new XMPPConne

我在使用smack库连接到Google服务器时使用了此代码。但问题是我不知道如何保持代码运行,以便我可以继续发送和接收消息。请帮助我。我正在复制粘贴代码:

// connect to gtalk server
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(connConfig);
connection.connect();

// login with username and password
connection.login("camel.test.1", "secret");

// set presence status info
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);

// send a message to somebody
Message msg = new Message("xxx.xxx@gmail.com", Message.Type.chat);
msg.setBody("hello");
connection.sendPacket(msg);

connection.disconnect();
请注意,发送消息与此代码配合良好。请告知我需要在同一代码中进行的修改,以便我可以将其用于发送和接收消息。

  • 不要让您的应用程序终止
  • 不要断开连接
  • 注册以侦听传入消息