Realization of landing function of instant chat system based on smack

Source: Internet
Author: User

Landing function Implementation: The main idea is: 1. Configure connection Information 2. Create an XMPP connection object; 3. Establish a connection; 4. Login

1. Configure connection Information

The primary is to configure the server IP and port number, the first parameter in the Connectionconfiguration () function is the server IP address, and the second parameter is the port number. The first parameter can also be written as the server hostname, and if it is written as a server hostname, you need to modify the client host to add the server IP address and its hostname in the hosts, as shown in. The path to the host file in the WINDOWS system is: C:\WINDOWS\system32\drivers\etc\hosts.


The code for configuring connection information is:

Org.jivesoftware.smack.ConnectionConfigurationconnConfig = new Org.jivesoftware.smack.ConnectionConfiguration (" Openfireserver ", 5222); this.config.setCompressionEnabled (true);                       Allow this.config.setReconnectionAllowed to be re            -connected (true);             This.config.setSendPresence (TRUE);


2. Establish an XMPP connection object

Create an XMPP Connection object with the Connconfig created in the previous step as a parameter

Create the code as:

Xmppconnection connection = newxmppconnection (connconfig);

3. Establish a connection

Call the Connect () method of the XMPP connection object to establish the connection.

The code is:

Connection.connect ();

4. Login

Log in with the pre-registered username and password as parameters for the login () method of the XMPP connection object. Note that the user name here must be written in the form of [email protected].

The Login code is:

Connection.login ("[Email protected]", "password");

The complete code snippet for user login is:

Public Intlogin (String userName, String PassWord, String serverName) {this.config = Newconnectionconfi              Guration (ServerName, 5222);                           This.config.setCompressionEnabled (TRUE);               Allow this.config.setReconnectionAllowed to be re-connected (true);               This.config.setSendPresence (TRUE);               Configure (Providermanager.getinstance ());                           Create Connection Object this.connection = Newxmppconnection (this.config);                     Connect, login try {this.connection.connect ();                                   This.connection.login (UserName, PassWord,                                                 Org.jivesoftware.spark.util.StringUtils.modifyWildcards (                     This.resource). Trim ()); This.sessionManager.setServerAddress (this.connection. GETservicename ());                     This.sessionManager.initializeSession (This.connection,username, PassWord);                      This.sessionManager.setJID (This.connection.getUser ()); Sparkmanager.getconnection (). Sendpacket (Newmucpacket ("logining", "Com.cetc32.muc", Spar                     Kmanager. Getsessionmanager (). getbareaddress ()));                     This.username = UserName;              return 0;              } catch (Xmppexception E1) {e1.printstacktrace ();       } return-1; }


Realization of landing function of instant chat system based on smack

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.