public class SendMessage{
private XMPPConnection con;
private ChatManager chatmanager;
public XMPPConnection login(String messageProducer, String messageProducerPassword) {
ConnectionConfiguration config = new ConnectionConfiguration(this.serverDomain,
this.serverPort);
XMPPConnection con = new XMPPConnection(config);
try {
con.connect();
} catch (XMPPException e) {
throw new RuntimeException("Connect xmpp server", e);
}
try {
con.login(this.messageProducer, this.messageProducerPassword);
} catch (XMPPException e) {
throw new RuntimeException("login failed: " + e.getMessage() + " with "
+ messageProducer + ":" + messageProducerPassword);
}
return con;
}
private void sendMessage(String to, String message) {
try {
Chat newChat = chatmanager.createChat(to, new MessageListener() {
public void processMessage(Chat chat, Message message) {
}
});
newChat.sendMessage(message);
} catch (XMPPException e) {
log.error("Got error while sending message to " + to.getUsername(), e);
}
}
private void layout() {
if (con != null && con.isConnected()) {
con.disconnect();
}
}
}
本文介绍了一种使用Java实现基于XMPP协议的即时通讯功能的方法。文中详细展示了如何通过XMPPConnection类完成服务器连接及登录验证,并发送消息给指定接收者。
1901

被折叠的 条评论
为什么被折叠?



