0

我正在使用此代码,我使用 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();

请注意,使用此代码发送消息可以正常工作。让我知道我需要在同一代码中进行的修改,以便我可以将其用于发送和接收消息。

4

1 回答 1

0
  • 不要让您的应用程序终止。
  • 不要断开连接。
  • 注册一个PacketListener来监听传入的消息。
于 2012-04-25T13:11:04.903 回答