Use Smack+openfire for instant messaging.

Source: Internet
Author: User

Starter: Personal Blog

Must note: Smack the latest 4.1.1, relative to the previous version of the change is very large, and the lack of information, official documents are not good, so the old version of 3.2.2 it. The code in this blog post is version 4.1.1, but it is not recommended.
Use OpenFire to do the server, with Spark to help debug the client, with smack (official document here, feel very bad) do Java library, to complete the instant communication function.

1. Installing OpenFire
Download and install on official website.

2. Install Spark
Download and install on official website.

3.maven Introduction of Smack

<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-java7</artifactId>
<version>4.1.1</version>
<exclusions>
<exclusion>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
<version>1.1.3.4.O</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-tcp</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-im</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-extensions</artifactId>
<version>4.1.1</version>
</dependency>

If you are prompted to install it manually, after locating the jar in the MAVEN repository and manually downloading it, execute a statement similar to this one:

MVN Install:install-file-dfile={path/to/your/ojdbc.jar}-dgroupid=com.oracle-dartifactid=ojdbc6-dversion=11.2.0- Dpackaging=jar

4. Write the demo code to do the experiment
Key concepts: Configuration, connectivity, roster, Session manager, session

Import java.util.Collection;

Import org.jivesoftware.smack.AbstractXMPPConnection;
Import Org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
Import org.jivesoftware.smack.SmackException.NotConnectedException;
Import Org.jivesoftware.smack.StanzaListener;
Import Org.jivesoftware.smack.chat.Chat;
Import Org.jivesoftware.smack.chat.ChatManager;
Import Org.jivesoftware.smack.chat.ChatManagerListener;
Import Org.jivesoftware.smack.chat.ChatMessageListener;
Import Org.jivesoftware.smack.filter.StanzaFilter;
Import Org.jivesoftware.smack.packet.Message;
Import org.jivesoftware.smack.packet.Presence;
Import Org.jivesoftware.smack.packet.Stanza;
Import Org.jivesoftware.smack.roster.Roster;
Import Org.jivesoftware.smack.roster.RosterListener;
Import org.jivesoftware.smack.tcp.XMPPTCPConnection;
Import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

public class Smackclient {

public static void Countpeople (roster r) {
System.out.println ("Online number changed to:" + r.getentrycount ());
}

public static void Go () {
try{
Configuration of the connection
Xmpptcpconnectionconfiguration.builder Builder = Xmpptcpconnectionconfiguration.builder ();
Builder.setusernameandpassword ("Guoguo", "cc19881031");
Builder.setservicename ("127.0.0.1");
Builder.sethost ("127.0.0.1");
Builder.setport (5222);
Do not add this line error, because there is no certificate
Builder.setsecuritymode (securitymode.disabled);
Builder.setdebuggerenabled (TRUE);
xmpptcpconnectionconfiguration config = Builder.build ();

Establish a connection and log in
Abstractxmppconnection C = new xmpptcpconnection (config);

C.addpacketsendinglistener (New Stanzalistener () {
@Override
public void Processpacket (Stanza st)
Throws Notconnectedexception {
System.out.println ("in Stanzalistener:" + st.getfrom ());
}
}, New Stanzafilter () {
@Override
Public Boolean accept (Stanza St) {
System.out.println ("in Stanzafilter:" + st.getfrom ());
return false;
}
});

C.connect ();
C.login ();

Final ROSTER roster = Roster.getinstancefor (c);
Presence P = roster.getpresence ("[email protected]");
System.out.println (P.gettype ());
Roster.addrosterlistener (New Rosterlistener () {
public void entriesadded (collection<string> arg0) {countpeople (roster);}
public void entriesdeleted (collection<string> addresses) {countpeople (roster);}
public void entriesupdated (collection<string> addresses) {countpeople (roster);}
public void presencechanged (presence presence) {Countpeople (roster);}
});

Set whether online status, and status description
Presence presence = new presence (Presence.Type.unavailable);
Presence.setstatus ("Gone fishing");
C.sendstanza (presence);

Session Manager setup and configuration monitoring
Chatmanager Chatmanager = Chatmanager.getinstancefor (c);
Chatmanager.addchatlistener (New Chatmanagerlistener () {
@Override
public void chatcreated (Chat cc, Boolean BB) {
Trigger a listener function when receiving messages from each other
Cc.addmessagelistener (New Chatmessagelistener () {
@Override
public void ProcessMessage (Chat cc, Message mm) {
System.out.println (Mm.getbody ());
}
});
}
});

Establish a session
Chat chat = chatmanager.createchat ("[email protected]");
Chat.getthreadid ();

Send Message
Message msg = new Message ();
Msg.setbody ("hello!");
Chat.sendmessage (msg);

while (true);
}catch (Exception e) {
E.printstacktrace ();
}
}

public static void Main (string[] args) {
Go ();
}
}

Use Smack+openfire for instant messaging.

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.