Android基于XMPP的即时通讯

闲暇之余,自己写了个简单的即时通讯,基于OpenFire服务器平台。

整个项目包括两个部分,一个是服务器端,一个是android手机端;

 

一、关于服务器端没什么好说的,下载安装配置即可

推荐下载带jar的程序安装,配置好java_jdk的可以使用压缩包解压安装

OpenFire下载地址:http://www.igniterealtime.org/downloads/index.jsp

顺便下载一个Spark,电脑端通讯,用于和你的安卓手机互发信息。

运行openFire截图

openfire的后台管理界面:

 

二、重点介绍一下android端的程序

1、首先要连接到OpenFire服务器,需要一个登陆界面,像这样

Android基于XMPP的即时通讯

连接服务器的代码:

Thread thread = new Thread(new Runnable() {

    public void run() {

        try {

            // 连接服务器

            XmppConnection.getConnection().login(userStr, passStr);

            // 连接服务器成功,更改在线状态

            Presence presence = new Presence(Presence.Type.available);

            XmppConnection.getConnection().sendPacket(presence);

            handler.sendEmptyMessage(1);

        } catch (XMPPException e) {

            XmppConnection.closeConnection();

            handler.sendEmptyMessage(2);

        }

    }

});

thread.start();

XmppConnection类的核心代码

public static int SERVER_PORT = 5222;//端口



public static String SERVER_HOST = "192.168.1.169";//服务器地址



public static String SERVER_NAME = "pearlemon";//服务器名称



private static XMPPConnection connection;



private static void openConnection() {

    try {

        if (null == connection || !connection.isAuthenticated()) {

            XMPPConnection.DEBUG_ENABLED = true;

            ConnectionConfiguration conConfig = new ConnectionConfiguration(

                    SERVER_HOST, SERVER_PORT, SERVER_NAME);

            conConfig.setReconnectionAllowed(true);

            conConfig.setSendPresence(true);

            conConfig.setSASLAuthenticationEnabled(true);

            connection = new XMPPConnection(conConfig);

            connection.connect();

            configureConnection();

        }

    } catch (XMPPException e) {



    }

}



public static XMPPConnection getConnection() {

    if (connection == null) {

        openConnection();

    }

    return connection;

}



public static void closeConnection() {

    connection.disconnect();

    connection = null;

}

2、连接到服务器之后,获取我们的好友,像这样

Android基于XMPP的即时通讯

获取好友列表的代码:

Thread thread = new Thread(new Runnable() {

    public void run() {

        try {

            XMPPConnection conn = XmppConnection.getConnection();

            Roster roster = conn.getRoster();

            friendList = new ArrayList<Map<String,String>>();

            Collection<RosterEntry> entries = roster.getEntries();

            HashMap<String, String> map = null;

            for (RosterEntry entry : entries) {

                map = new HashMap<String, String>();

                map.put("Name", entry.getName());

                friendList.add(map);

            }

            handler.sendEmptyMessage(0);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

});

thread.start();

3、聊天界面的程序,像这样

Android基于XMPP的即时通讯

Spark聊天界面

接受消息的代码:

private ChatManager chatMan;

private Chat newchat;
// 消息监听

chatMan = XmppConnection.getConnection().getChatManager();

newchat = chatMan.createChat(toUserID, null);

chatMan.addChatListener(new ChatManagerListener() {

    @Override

    public void chatCreated(Chat chat, boolean able) {

        chat.addMessageListener(new MessageListener() {

            @Override

            public void processMessage(Chat chat, Message message) {

                // 收到来自pc服务器的消息(获取自己好友发来的信息)

                if (message.getFrom().contains(toUserID)) {

                    if (message.getBody().length() > 0) {

                        // 获取用户、消息、时间、IN

                        String[] args = new String[] { toUserID,

                                message.getBody() };

                        // 在handler里取出来显示消息

                        android.os.Message msg = handler

                                .obtainMessage();

                        msg.what = 1;

                        msg.obj = args;

                        msg.sendToTarget();

                    }

                }

            }

        });

    }

});

发送消息的代码:

// 发送消息

String fromUserID = mAppGlobal.getName();

String dateStr = DateTimeUtils.formatDate(new Date());

chatList.add(new Msg(dateStr, fromUserID, content, "OUT"));

// 刷新适配器

mAdapter.notifyDataSetChanged();

mListView.setSelection(ListView.FOCUS_DOWN);//刷新到底部 

Thread thread = new Thread(new Runnable() {



    @Override

    public void run() {

        try {

            newchat = chatMan.createChat(toUserID + "@8nqa3d40s88hspl", null);

            Message msg = new Message();

            msg.setBody(content);

            // 发送消息

            newchat.sendMessage(msg);

        } catch (XMPPException e) {

            e.printStackTrace();

        }

    }

});

thread.start();

etSend.setText("");

基本就是这些东西了,源码送上:http://files.cnblogs.com/files/pear-lemon/XmppTest.zip

你可能感兴趣的:(android)