OpenFire客户端编程示例


import java.util.Collection;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;

public class SimpleClient {

/** Server name */
public static final String SERVER_NAME = "localhost";
/** Server port */
public static final int SERVER_PORT = 5222; // as you can see in Admin
// Console properties

/** Client name - for login */
private final String clientName;
/** Client password - for login*/
private final String clientPassword;
/** Client color - for writing in color */
private final String clientColor;
/** Chat friend */
private final String friendName;

/**
* Constrcuts a new SimpleClient
* @param clientName0 -
* @param clientPassword0 -
* @param clientColor0 -
* @param friendName0 -
*/
public SimpleClient(String clientName0, String clientPassword0, String clientColor0, String friendName0) {
super();
this.clientName = clientName0;
this.clientPassword = clientPassword0;
this.clientColor = clientColor0;
this.friendName = friendName0;
}

/**
* Main process.
*/
public void run() {
try {
this.runImpl();
} catch (XMPPException e) {
System.err.println("Exception: " + e);
}

}

private void runImpl() throws XMPPException {
XMPPConnection.DEBUG_ENABLED = true;

/*
* Configuration
*/
final ConnectionConfiguration config = new ConnectionConfiguration(
SimpleClient.SERVER_NAME, SimpleClient.SERVER_PORT);
config
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setCompressionEnabled(false);


/*
* Open Connection
*/
final XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login(this.clientName, this.clientPassword);

/*
* Chat
*/
final MessageListener messageListner = new SimpleMessageListner();
final Chat chat = connection.getChatManager().createChat(this.friendName, messageListner);
final java.util.Date date = new java.util.Date(System.currentTimeMillis());
final String msgSent = " [sent by " + this.clientName + ", at " + date.toString() + "]";
final Message newMessage = new Message();
newMessage.setBody("Howdy (colored)!" + msgSent);
newMessage.setProperty("favoriteColor", this.clientColor);
chat.sendMessage(newMessage);
chat.sendMessage("Howdy!" + msgSent);

/*
* Roster
*/
final Roster roster = connection.getRoster();
// this is already the default Mode but added just for the example
Roster
.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);
roster.createEntry(this.friendName, "", null);

this.presenceStatus(roster, "unknown@someone");
this.presenceStatus(roster, this.friendName);

final Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
System.out.println("RosterEntry " + entry);
}

final RosterListener rosterListener = new SimpleRosterListner();
roster.addRosterListener(rosterListener);

/*
* End connection
*/
connection.disconnect();
}

private void presenceStatus(final Roster roster, String user) {
final Presence presence = roster.getPresence(user);
final String response = presence == null ? "Not around" : presence.getStatus();
System.out.println("Presence of '" + user + "': " + response);
}

/**
*
* @author some
*
*/
private class SimpleMessageListner implements MessageListener {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: "
+ message.getBody());
}
}

/**
*
* @author some
*
*/
private class SimpleRosterListner implements RosterListener {
// Ignored events
public void entriesAdded(Collection<String> addresses) {
}

public void entriesDeleted(Collection<String> addresses) {
}

public void entriesUpdated(Collection<String> addresses) {
}

public void presenceChanged(Presence presence) {
System.out.println("Presence changed: "
+ presence.getFrom() + " " + presence);
}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值