关于Smack编程库,前面我们提到,它是面向Java端的api,主要在PC上使用,利用它我们可以向openfire服务器注册用户,发送消息,并且可以通过监听器获得此用户的应答消息,以及构建聊天室,分组,个人通讯录等等。
下面我们写几个程序小例子测试一下。
(1)登录操作
PPConnection.DEBUG_ENABLED = true;
AccountManager accountManager;
final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
// 允许自动连接
connectionConfig.setReconnectionAllowed(true);
connectionConfig.setSendPresence(true);
Connection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();// 开启连接
accountManager = connection.getAccountManager();// 获取账户管理类
} catch (XMPPException e) {
throw new IllegalStateException(e);
}
// 登录
connection.login("admin", "admin","SmackTest");
System.out.println(connection.getUser());
connection.getChatManager().createChat("shimiso@csdn.shimiso.com",null).sendMessage("Hello word!");
运行结果:
在login中一共有三个参数,登录名,密码,资源名,可能有人不明白资源名到底是什么意思,其实就是客户端的来源,客户端的名称,如果不写它默认就叫smack,如果你用相同的账户不同的资源名和同一个人发三条消息,那将会弹出三个窗口,而不是一个窗口。
同时smack还为我们提供了非常好的调试工具Smack Debug,利用该工具我们可以准确的捕获详细的往返报文信息。
(2)下面我们继续写个聊天的例子:
PPConnection.DEBUG_ENABLED = true;
AccountManager accountManager;
final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
// 允许自动连接
connectionConfig.setReconnectionAllowed(true);
connectionConfig.setSendPresence(true);
Connection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();// 开启连接
accountManager = connection.getAccountManager();// 获取账户管理类
} catch (XMPPException e) {
throw new IllegalStateException(e);
}
// 登录
connection.login("admin", "admin","SmackTest3");
ChatManager chatmanager = connection.getChatManager();
Chat newChat = chatmanager.createChat("shimiso@csdn.shimiso.com", new MessageListener() {
public void processMessage(Chat chat, Message message) {
if (message.getBody() != null) {
System.out.println("Received from 【"
+ message.getFrom() + "】 message: "
+ message.getBody());
}
}
});
Scanner input = new Scanner(System.in);
while (true) {
String message = input.nextLine();
newChat.sendMessage(message);
}
运行结果:
这里我们用Scanner来捕捉用户在控制台的键盘操作,将信息发出,同时创建了一个MessageListener监听,在其中强制实现processMessage方法即可捕获发回的信息,在初次使用上还是较为容易上手的,我们只要细心查看API即可逐步深入下去。
(3)除了聊天以外我们经常还能想到就是广播
需要给所有在线的用户发送一个通知,或者给所有在线和离线的用户全发送,我们先演示如何给在线用户发送一个广播:
PPConnection.DEBUG_ENABLED = false;
AccountManager accountManager;
final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
// 允许自动连接
connectionConfig.setReconnectionAllowed(true);
connectionConfig.setSendPresence(true);
Connection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();// 开启连接
accountManager = connection.getAccountManager();// 获取账户管理类
} catch (XMPPException e) {
throw new IllegalStateException(e);
}
connection.login("admin", "admin","SmackTest3");
Message newmsg = new Message();
newmsg.setTo("shimiso@csdn.shimiso.com");
newmsg.setSubject("重要通知");
newmsg.setBody("今天下午2点60分有会!");
newmsg.setType(Message.Type.headline);// normal支持离线
connection.sendPacket(newmsg);
connection.disconnect();
运行结果:
将参数设置为Message.Type.normal即可支持离线广播,openfire系统会自动判断该用户是否在线,如果在线就直接发送出去,如果不在线则将信息存入ofoffline表,现在我将shimiso用户退出登录,再给它发消息,我们可以进入openfire库的ofoffline表中,非常清楚看到里面躺着一条离线消息记录