基于XMPP openfire smack开发之smack类库介绍和使用[2]
关于Smack编程库,前面我们提到,它是面向Java端的api,主要在PC上使用,利用它我们可以向openfire服务器注册用户,发送消息,并且可以通过监听器获得此用户的应答消息,以及构建聊天室,分组,个人通讯录等等。
下面我们写几个程序小例子测试一下。
(1)登录操作
[java] view plaincopy
1. PPConnection.DEBUG_ENABLED = true;
2. AccountManager accountManager;
3. finalConnectionConfiguration connectionConfig = newConnectionConfiguration(
4. "192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
5.
6. // 允许自动连接
7. connectionConfig.setReconnectionAllowed(true);
8. connectionConfig.setSendPresence(true);
9.
10. Connection connection = newXMPPConnection(connectionConfig);
11. try{
12. connection.connect();// 开启连接
13. accountManager = connection.getAccountManager();// 获取账户管理类
14. } catch(XMPPException e) {
15. thrownewIllegalStateException(e);
16. }
17.
18. // 登录
19. connection.login("admin", "admin","SmackTest");
20. System.out.println(connection.getUser());
21. connection.getChatManager().createChat("shimiso@csdn.shimiso.com",null).sendMessage("Hello word!");
运行结果:
在login中一共有三个参数,登录名,密码,资源名,可能有人不明白资源名到底是什么意思,其实就是客户端的来源,客户端的名称,如果不写它默认就叫smack,如果你用相同的账户不同的资源名和同一个人发三条消息,那将会弹出三个窗口,而不是一个窗口。
同时smack还为我们提供了非常好的调试工具Smack Debug,利用该工具我们可以准确的捕获详细的往返报文信息。
(2)下面我们继续写个聊天的例子:
[java] view plaincopy
1. PPConnection.DEBUG_ENABLED = true;
2. AccountManager accountManager;
3. finalConnectionConfiguration connectionConfig = newConnectionConfiguration(
4. "192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
5.
6. // 允许自动连接
7. connectionConfig.setReconnectionAllowed(true);
8. connectionConfig.setSendPresence(true);
9.
10. Connection connection = newXMPPConnection(connectionConfig);
11. try{
12. connection.connect();// 开启连接
13. accountManager = connection.getAccountManager();// 获取账户管理类
14. } catch(XMPPException e) {
15. thrownewIllegalStateException(e);
16. }
17.
18. // 登录
19. connection.login("admin", "admin","SmackTest3");
20. ChatManager chatmanager = connection.getChatManager();
21. Chat newChat = chatmanager.createChat("shimiso@csdn.shimiso.com", newMessageListener() {
22. publicvoidprocessMessage(Chat chat, Message message) {
23. if(message.getBody() != null) {
24. System.out.println("Received from 【"
25. + message.getFrom() + "】 message: "
26. + message.getBody());
27. }
28.
29. }
30. });
31. Scanner input = newScanner(System.in);
32. while(true) {
33. String message = input.nextLine();
34. newChat.sendMessage(message);
35. }
运行结果:
这里我们用Scanner来捕捉用户在控制台的键盘操作,将信息发出,同时创建了一个MessageListener监听,在其中强制实现processMessage方法即可捕获发回的信息,在初次使用上还是较为容易上手的,我们只要细心查看API即可逐步深入下去。
(3)除了聊天以外我们经常还能想到就是广播
需要给所有在线的用户发送一个通知,或者给所有在线和离线的用户全发送,我们先演示如何给在线用户发送一个广播:
[java] view plaincopy
1. PPConnection.DEBUG_ENABLED = false;
2. AccountManager accountManager;
3. finalConnectionConfiguration connectionConfig = newConnectionConfiguration(
4. "192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
5.
6. // 允许自动连接
7. connectionConfig.setReconnectionAllowed(true);
8. connectionConfig.setSendPresence(true);
9.
10. Connection connection = newXMPPConnection(connectionConfig);
11. try{
12. connection.connect();// 开启连接
13. accountManager = connection.getAccountManager();// 获取账户管理类
14. } catch(XMPPException e) {
15. thrownewIllegalStateException(e);
16. }
17. connection.login("admin", "admin","SmackTest3");
18. Message newmsg = newMessage();
19. newmsg.setTo("shimiso@csdn.shimiso.com");
20. newmsg.setSubject("重要通知");
21. newmsg.setBody("今天下午2点60分有会!");
22. newmsg.setType(Message.Type.headline);// normal支持离线
23. connection.sendPacket(newmsg);
24. connection.disconnect();
运行结果:
将参数设置为Message.Type.normal即可支持离线广播,openfire系统会自动判断该用户是否在线,如果在线就直接发送出去,如果不在线则将信息存入ofoffline
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
,现在我将shimiso用户退出登录,再给它发消息,我们可以进入openfire库的ofoffline表中,非常清楚看到里面躺着一条离线消息
记录
混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载
是发给shimiso这个用户的
(4)那么我们如何让shimiso这个用户一登陆就取到离线消息呢?
请看如下代码
[java] view plaincopy
1. PPConnection.DEBUG_ENABLED = false;
2. AccountManager accountManager;
3. finalConnectionConfiguration connectionConfig = newConnectionConfiguration(
4. "192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
5.
6. // 允许自动连接
7. connectionConfig.setReconnectionAllowed(true);
8. connectionConfig.setSendPresence(false);//不要告诉服务器自己的状态
9. Connection connection = newXMPPConnection(connectionConfig);
10. try{
11. connection.connect();// 开启连接