Smack+Spark+Openfire集成环境IM开发

本文详细介绍使用Smack 4.1+版本集成Openfire服务器的过程。从环境搭建到核心代码实现,包括Openfire的安装配置及Smack类库选择,并提供完整示例代码,演示如何实现基于XMPP协议的消息发送接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、相关知识背景介绍
1. IM(InstantMessaging),即实时通讯,这是一种类似与QQ、微信等的通讯方式。

2.Smack 是一个开源的,易于使用的XMPP客户端类库,是使用java编写的,也是Spark软件的的核心技术。

3.openfire采用java开发,开源的实时协作服务器基于XMPP协议。Openfire的安装和使用非常简单,并且有自己的Web管理平台。

4.要进行这样的集成开发,我们需要准备的就是安装好Openfire服务器,然后根据需要使用Smack类库编写我们需要的客户端程序。

5.本次开发使用 Smack 4.1+ 版本

二、环境搭建

  1. Openfire的下载和安装
    官方下载地址 : http://www.igniterealtime.org/downloads/index.jsp
    安装和配置 : http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html

  2. Smack 类库的选择
    这是我引用的Smack类库:
    这里写图片描述

准备好这些,我们就要开始使用Smack API 进行IM编程了。
我将Smack代码放进线程中,重要的步骤会有注释,具体步骤和实现如下,

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    /**1.配置相關參數*/
                    XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                            .setUsernameAndPassword(userName, password).setServiceName(serviceName).setHost(host)
                            .setPort(servicePort).setSecurityMode(SecurityMode.disabled).build();
                    final AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);

                    /**2.建立連接*/
                    conn2.connect();
                    conn2.login("userName", "password");// 登錄賬號和密碼

                    ChatManager chatmanager = ChatManager.getInstanceFor(conn2);
                    /** 3.添加消息監聽器,獲取消息 */
                    chatmanager.addChatListener(new ChatManagerListener() {
                        @Override
                        public void chatCreated(Chat chat, boolean createdLocally) {
                            chat.addMessageListener(new ChatMessageListener() {
                                @Override
                                public void processMessage(Chat chat, Message message) {
                                    if (message.getType() == Message.Type.chat || message.getType() == Message.Type.normal) {
                                        if (message.getBody() != null) {
                                            /**回復消息的格式*/
                                            System.out.println(message.getFrom() + " : " + message.getBody());
                                        }
                                    }
                                }
                            });
                        }
                    });

                    /**4.建立会话*/
                    Chat newChat = chatmanager.createChat(toJid);

                    /** 用戶輸入 */
                    Scanner input = new Scanner(System.in);
                    while (true) {
                        String message = input.nextLine();
                        newChat.sendMessage(message);
                    }

                } catch (SmackException e) {
                    System.out.println("SmackException");
                    e.printStackTrace();
                } catch (IOException e) {
                    System.out.println("IOException");
                    e.printStackTrace();
                } catch (XMPPException e) {
                    System.out.println("XMPPEXception");
                    e.printStackTrace();
                }
            }

        }).start();

实现效果展示:
这里写图片描述

参考博文
http://blog.youkuaiyun.com/qinnimei/article/details/50469087
http://blog.youkuaiyun.com/shimiso/article/details/8816540
http://www.eoeandroid.com/forum.php?

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值