按照书上,一步步实现一个IM系统,虽然没有完好的设计,但是还是体验了一下RCP开发一个完整软件的过程:) 但书上有一处错误,搞到我调了N久……在10.3节的扩展org.eclipse.core.runtime.adapters中,书上只列出了两个对象的adapter配置,即:只配置了RosterEntry和RosterGroup,却没有对Roster对象进行adapter,找出随书附带的源码,发现里面的确配置了Roster。
看书的时候不能光顾着看文字,还得结合附带的代码……
J2EE, Enterprise Solution, Web development, Software Engineer, My ideas ...
package org.prc;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
/**
* Chat prototype using smack as client library. Server side are supported by
* openfire.
*
* @author promise
* @date 2009-3-30
*
*/
public class FireChat {
public static XMPPConnection con;
public static Chat newChat;
public static ChatManager chatmanager;
public static void main(String[] args) throws InterruptedException {
// XMPPConnection.DEBUG_ENABLED = true;
try {
// Create a connection to server
ConnectionConfiguration config = new ConnectionConfiguration(
"localhost", 5222);
con = new XMPPConnection(config);
// connect and login with the username and pwd on server
con.connect();
con.login("promise", "promise");
System.out.println("Authenticated = " + con.isAuthenticated());
// add a listener to receive all messages
addListener();
chatmanager = con.getChatManager();
newChat = chatmanager.createChat("prc@fantasy-promise/spark",
new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("I'm sending: "
+ message.getBody());
}
});
newChat.sendMessage("hi");
} catch (XMPPException e) {
e.printStackTrace();
} finally {
Thread.sleep(3600000);
con.disconnect();
}
}
private static void addListener() {
// just need Messages :)
PacketFilter filterMessage = new PacketTypeFilter(Message.class);
PacketListener myListener = new PacketListener() {
public void processPacket(Packet packet) {
System.out.println("From: " + packet.getFrom() + "\n");
System.out.println("Body: " + ((Message) packet).getBody());
// when receiving prc's Message, just say something else again
// and again, robot :)
try {
newChat.sendMessage("hi again :)");
} catch (XMPPException e) {
e.printStackTrace();
}
}
};
// register the listener to the connection
con.addPacketListener(myListener, filterMessage);
}
}