Source code example for sending instant messages based on the Openfire Message Server application Smack

Source: Internet
Author: User

Environment preparation: 1. install the openfire server and configure related items. introduce smack * in the project *. the jar package source code is as follows: [java] package com. xjtu. jointframe. im. util; import java. util. arrayList; import java. util. collection; import java. util. iterator; import java. util. list; import org. jivesoftware. smack. chat; import org. jivesoftware. smack. connection; import org. jivesoftware. smack. connectionConfiguration; import org. jivesoftware. smack. messageListener; import org. jivesof Tware. smack. roster; import org. jivesoftware. smack. rosterEntry; import org. jivesoftware. smack. XMPPConnection; import org. jivesoftware. smack. XMPPException; import org. jivesoftware. smack. packet. message;/***** <p> * Description: example business class * </p> ** <p> * Copyright: Copyright (c) 2012 * </p> *** @ author dml @ 2013-2-21 * @ version 1.0 */www.2cto. compublic class IMServer {private static final String IP = "127.0.0.1"; private static final Integer DK = 5222; private static Roster roster;/*** get connection *** @ return Connection */public static connection getConnection () {ConnectionConfiguration config = new ConnectionConfiguration (IP, DK); Connection connection = new XMPPConnection (config); return connection ;} /*** send a notification message ** @ param username * @ param pass * @ param messgage * @ param groupName * @ th Rows XMPPException */public void SendBCMsg (String username, String pass, String messgage, String groupName) throws XMPPException {Connection con = IMServer. getConnection (); con. connect (); con. login (username, pass); Message m = new Message (); m. setBody (messgage); // sets the message. M. setTo (groupName + "@ broadcast. xjtu "); // [groupname] @ [serviceName]. [serverName] con. sendPacket (m);}/*** send instant message ** @ param username * @ param pass * @ param messgage * @ throws XMPPException */public void SendMsg (String username, string pass, String messgage) throws XMPPException {Connection con = IMServer. getConnection (); con. connect (); con. login (username, pass); Chat mychat = con. getChatManager (). createChat ("admin @ xjtu", // JID of the acceptor. JID is the new MessageListener () {@ Override public void processMessage (Chat chat, Message message) of the domain to be added) {String messageBody = message. getBody (); System. out. println ("received message:" + messageBody + "" + message. getFrom () ;}}); mychat. sendMessage (messgage); // send message con. disconnect (); // disconnect}/*** get friend List ** @ param username * @ param pass * @ return * @ throws XMPPException */public List <RosterEntry> getRosterList (String username, string pass) throws XMPPException {Connection con = IMServer. getConnection (); con. connect (); con. login (username, pass); Collection <RosterEntry> rosters = con. getRoster (). getEntries (); for (RosterEntry rosterEntry: rosters) {System. out. print ("name:" + rosterEntry. getName () + ", jid:" + rosterEntry. getUser (); // you can obtain the JID System. out. println ("");} return null;}/*** get the user list (including group information) ** @ param username * @ param pass * @ return * @ throws XMPPException */public List <RosterEntry> getRoster (String username, String pass) throws XMPPException {Connection con = IMServer. getConnection (); con. connect (); con. login (username, pass); roster = con. getRoster (); List <RosterEntry> EntriesList = new ArrayList <RosterEntry> (); Collection <RosterEntry> rosterEntry = roster. getEntries (); Iterator <RosterEntry> I = rosterEntry. iterator (); while (I. hasNext () {EntriesList. add (I. next ();} return EntriesList ;}}

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.