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