Technical Note: The Openfire+spark+smack of XMPP

Source: Internet
Author: User
Tags getmessage

In the field of instant messaging, only one XMPP protocol is currently found, and there are many mature products on the basis of their protocols, and they are open source. So I want to know more about it in this field.

XMPP Protocol: The specific concept I will not write, after all, this thing is everywhere on the internet. Simply put, it is an XML-based protocol. What problem does it solve? is to set standards for instant messaging, as long as you follow the standard to complete the function of instant communication. The advantage of the standard is that there can be a variety of different implementations, everyone in this standard development of their own expertise. It also provides the basis for the interconnection of instant communication. XMPP protocol According to the internet is still relatively good, performance is Google and other large companies in their own instant messaging products.

OpenFire: It's a little red that I found in this million-flower bush. The main reasons for choosing it are:

1. It supports XMPP

2, it is implemented in Java, open-source

3, it is more famous, good performance

4, the deployment is very simple, there are many ready-made plug-ins

As for the installation and deployment of OpenFire online articles more, here also do not intend to write. You can find more resources on this website: http://www.igniterealtime.org/

Why is it that OpenFire deployment is simple, I just downloaded an EXE installation package, and then after Windows followed the wizard to install the deployment configuration, then the entire client can chat. And there are matching client products on the Igniterealtime, as well as a class library. It is convenient and practical for developers.

However, I spent more time studying is not the service side, after all, this part has not yet come to the actual product development, as long as can run. So where is the time spent? Is the client, after all, spark this client interface is a bit old, and the chat function is also relatively primitive. So you have to spend more time on it.

In addition, the actual requirements of the customer requirements are applicable to multi-platform: Windows,macos,android,ios, and even in the browser. After all, there was no instant messaging products, this is a bit of a little to take. So had to contact each other to understand. After understanding the real can be used for commercial things are not many, only to customize the development of their own. Here's an official XMPP website to look at: http://xmpp.org/software, which lists some of the existing products and development packages. including Clients,servers,libraries,projects. OpenFire can be found in servers, spark can be found in clients, smack found in libraries. Projects lists some of the larger items that use the XMPP protocol, such as Google, WhatApp, and Apple, which are big enough.

My idea is to start around Igniterealtime, which is a continuous maintenance product line.

1, spark can be used on multi-platform, so the PC side is solved

2, Smack package can support Android platform, can be based on this package to customize the development of Android products

Spark's functionality is still relatively complete, text, images, file messages are supported, single chat, group chat is also supported. As long as the interface to do some customization can be used. And Spark's communication core is smack, which also fully illustrates the importance of smack. Smack new version already supports Android, so I wrote a smack Android version of the demo, on the one hand to understand smack, but also understand the development of Android.

The source code has been placed on GitHub: Https://github.com/mini188/SmackDemo

Complete the very rub, that is, a login, simple chat.

Simply write a singleton class: Xmppconnectionservice, mainly responsible for the communication management with XMPP server, the most important thing is to build a connection, because the Smack (4.1.x) version of the update and before the old API is different. So I see a lot of information on the internet is relatively old writing, tried for some time is a failure. Later find the method:

     Public voidConnect (String userName, string pwd) {if(_xmppconnection! =NULL&&_xmppconnection.isconnected ()) {            return; } xmpptcpconnectionconfiguration.builder Configbuilder=Xmpptcpconnectionconfiguration.builder (); Configbuilder.sethost ("192.168.45.41"); Configbuilder.setport (5222);        Configbuilder.setusernameandpassword (UserName, PWD); Configbuilder.setservicename ("ZRTC");        Configbuilder.setcustomsslcontext (Getsslcontent ()); //Configbuilder.setsecuritymode (ConnectionConfiguration.SecurityMode.disabled);_xmppconnection=Newxmpptcpconnection (Configbuilder.build ()); NewThread () {@Override Public voidrun () {Try{_xmppconnection.connect ();                _xmppconnection.login (); } Catch(IOException e) {LOG.E ("Xmpp_connlogin", E.getmessage ()); } Catch(xmppexception e) {LOG.E ("Xmpp_connlogin", E.getmessage ()); } Catch(smackexception e) {LOG.E ("Xmpp_connlogin", E.getmessage ());    }}}.start (); }

The main point in this code is two note:

1, server communication to use a custom certificate to use the custom sslconent, white point is not to verify whether the certificate is trustworthy. This thing doesn't know if using a CA-signed certificate will have this problem.

2, connect to use the thread to execute, this is because Android does not allow the main thread to run this time-consuming code, affecting the interface response

Get a list of friends who are logged in:

Roster roster = roster.getinstancefor (_conn); Set<RosterEntry> entries = roster.getentries ();

To create a chat session:

    Private void createchat (Intent Intent) {        = Intent.getstringextra (JID);         = _chatmgr.createchat (_jid);        _chat.addmessagelistener (new  Chatmessagelistener () {            @Override            public  void  processmessage (chat chat, message message) {                addmessage (message);            }        });    

The session can also listen for messages so that they can be processed by sending messages. For example, shown in the chat log.

Send Chat message:

_sendbutton.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {EditText SendText=(EditText) Findviewbyid (r.id.textinput); String msg=Sendtext.gettext (). toString (); if(!msg.equals ("") {Message message=NewMessage ();                    Message.setto (_jid);                    Message.setbody (msg);                    Message.settype (Message.Type.chat);                    AddMessage (message); Try{_chat.sendmessage (message); } Catch(smackexception.notconnectedexception e) {e.printstacktrace (); } sendtext.settext (""); }            }        });

or through session _chat, it has a method SendMessage can send the message out, quite simple.

Finally paste the effect of the chat, really good rub:

Last note, this Android client demo code references an open source project: Https://github.com/siacs/Conversations

Technical Note: The Openfire+spark+smack of XMPP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.