Smack API Reprint not tested

Source: Internet
Author: User

===============================================================
Proactively send information to a user
------------------------------------
Xmppconnection.debug_enabled = true;
Set server address
Xmppconnection conn = new Xmppconnection ("127.0.0.1");
Conn.connect ();
Enter your account and password to log in
Conn.login ("[Email protected]", "123456");
Create a conversation with Test1 and set up the information listener
Chat mychat = Conn.getmanager (). Create ("[email protected]",
New MessageListener () {
@Override
public void ProcessMessage (chat chat, message message) {
String messagebody = Message.getbody ();
System.out.println ("received message:" +messagebody);
}
});
Send to test1 text message
Mychat.sendmessage ("Hello");
Exit Login
Conn.disconnect ();
===============================================================
Set your own login status
------------------------------------
Xmppconnection.debug_enabled = true;
Set server address
Xmppconnection conn = new Xmppconnection ("127.0.0.1");
Conn.connect ();
Enter your account and password to log in
Conn.login ("[Email protected]", "123456");

Set personal status information after logging in
Presence P = new Presence (Presence.Type.available);
P.setstatus ("In a daze ... ");
Conn.sendpacket (P);

Exit Login
Conn.disconnect ();
==========================================================================
Passively receive messages from users
------------------------------------
Xmppconnection.debug_enabled = true;
Set server address
Xmppconnection conn = new Xmppconnection ("127.0.0.1");
Conn.connect ();
Enter your account and password to log in
Conn.login ("[Email protected]", "123456");
Setting up monitoring of information
Conn.getchatmanager (). Addchatlistener (New Chatmanagerlistener () {
@Override
public void chatcreated (chat chat, Boolean createdlocally) {
Chat.addmessagelistener (New MessageListener () {
@Override
public void ProcessMessage (chat chat, message message) {
String messagebody = Message.getbody ();
System.out.println ("Received information:" +messagebody);
}
});
}
});
Exit Login
Conn.disconnect ();
==========================================================================
Get a list of my friends
------------------------------------
Set server address
Xmppconnection conn = new Xmppconnection ("127.0.0.1");
Conn.connect ();
Enter your account and password to log in
Conn.login ("[Email protected]", "123456");
collection<rosterentry> rosters = Conn.getroster (). GetEntries ();
System.out.println ("My Friends list: =======================");
for (Rosterentry rosterentry:rosters) {
System.out.print ("Name:" +rosterentry.getname () + ", Jid:" +rosterentry.getuser ());
System.out.println ("");
}
System.out.println ("My Friends list: =======================");

Conn.disconnect ();
==========================================================================
Chat window input status, using XEP-0085 protocol
------------------------------------
Send to test1 initial text message with input status
Message mess = new Message ();
Mess.addextension (New Chatstateextension (chatstate.active));
Mychat.sendmessage (mess);
Send to test1 initial text message with the state being entered
Message mess = new Message ();
Mess.addextension (New Chatstateextension (chatstate.composing));
Mychat.sendmessage (mess);

Send to test1 initial text message with paused input status
Message mess = new Message ();
Mess.addextension (New Chatstateextension (chatstate.paused));
Mychat.sendmessage (mess);
Other slightly ...

When it is received
public void ProcessMessage (chat chat, message message) {
String messagebody = Message.getbody ();
Packetextension PE;

PE = message.getextension ("Composing", "http://jabber.org/protocol/chatstates");
if (PE! = null) {
System.out.println ("The other party is entering ...");
}

PE = message.getextension ("Active", "http://jabber.org/protocol/chatstates");
if (PE! = null) {
System.out.println ("Received information:" +messagebody);
}

PE = message.getextension ("Paused", "http://jabber.org/protocol/chatstates");
if (PE! = null) {
System.out.println ("The other party has paused the input");
}

PE = message.getextension ("Inactive", "http://jabber.org/protocol/chatstates");
if (PE! = null) {
System.out.println ("The other chat window loses focus");
}

PE = message.getextension ("Gone", "http://jabber.org/protocol/chatstates");
if (PE! = null) {
System.out.println ("The other chat window is closed");
}
}
==========================================================================
Receive invitations, join multiplayer chat rooms
------------------------------------
Multiuserchat.addinvitationlistener (conn, new Invitationlistener () {
@Override
public void invitationreceived (Xmppconnection conn, String,
String inviter, String reason, string password, message message) {
Multiuserchat multiuserchat = new Multiuserchat (conn, guest);
System.out.println ("received a chat room invitation from" +inviter+ ". Invitation accompanying content: "+reason);
try {
Multiuserchat.join ("Test2", password);
} catch (Xmppexception e) {
System.out.println ("Failed to join the chat room");
E.printstacktrace ();
}
System.out.println ("Successfully joined the chat room");
Multiuserchat.addmessagelistener (New Packetlistener () {
@Override
public void Processpacket (Packet Packet) {
Message message = (message) packet;
Receive chat information from a chat room
System.out.println (Message.getfrom () + ":" +message.getbody ());
}
});
}
Send Message to chat room
Multiuserchat.sendmessage ("The novice arrives, everybody cares!") ");
});

==========================================================================
Login Gtalk
------------------------------------
Xmppconnection conn = new Xmppconnection (New Connectionconfiguration ("talk.google.com", 5222, "gmail.com"));
Conn.connect ();
Enter the Gtalk account password
Conn.login ("88888888", "8888888888");
collection<rosterentry> rosters = Conn.getroster (). GetEntries ();
Get a list of friends on Gtalk
System.out.println ("My Friends list: =======================");
for (Rosterentry rosterentry:rosters) {
System.out.print ("Name:" +rosterentry.getname () + ", Jid:" +rosterentry.getuser ());
System.out.println ("");
}
System.out.println ("My Friends list: =======================");

Conn.disconnect ();
==========================================================================

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.