XMPP Client library Smack development of 4.0.6 version five

Source: Internet
Author: User

XMPP Client library Smack development of 4.0.6 version five

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

Iv. using Chat message communication

Sending and receiving messages back and forth is the core function of instant Messaging. Although a single message is sent and received in the form of a packet, it is usually treated as a message string for chatting, using the Org.jivesoftware.smack.Chat class.

1. Chat class

One chat chat creates a message thread (through the thread ID) between two users. The following code snippet shows how to create a new chat and then send a text message to the user:

Suppose you have created a xmppconnection named "Connection"

Chatmanagerchatmanager = Connection.getchatmanager ();

Chatnewchat = Chatmanager.createchat ("[Email protected]", Newmessagelistener () {

public void ProcessMessage (Chat chat,message Message) {

System.out.println ("Receivedmessage:" + message);

}

});

try{

Newchat.sendmessage ("howdy!");

}catch (Xmppexceptione) {

System.out.println ("Error deliveringblock");

}

The Chat.sendmessage (string) method makes it easy to create a message object, set the message body body with a string parameter, and then send a message. In some cases you may want to set additional values before sending messages, using the Chat.createmessage () method and the Chat.sendmessage (message) method, as shown in the following code snippet:

Messagenewmessage = new Message ();

Newmessage.setbody ("howdy!");

Message.setproperty ("FavoriteColor", "Red");

Newchat.sendmessage (Newmessage);

In the previous example, we can notice that a message listener MessageListener is specified when chatting chat is created, and the message listener is notified at any time when a chat message from another user arrives. The following code snippet uses a listener to make a parrot, which echoes messages that are passed from other users.

Suppose a message listener has been set up in chat chats MessageListener

Publicvoid ProcessMessage (chat chat, message message) {

Send the message content sent by the user to the user

Chat.sendmessage (Message.getbody ());

}

2. Call Chat

When you are prompted to have another user's chat message come up, the settings are slightly different because you are receiving a chat message for the first time. Instead of explicitly creating a chat to send a message, when Chatmanager creates a chat instance, you need to register to process the newly created chat instance. Chatmanager will find a matching chat through the thread ID, and if chat does not exist, it will create a new chat object to match. To get this new chat, you must register to be notified. You can register a message listener to receive all incoming messages.

Assume that a xmppconnection named "Connection" has been created

CHATMANAGERCM = Connection.getchatmanager (). Addchatlistener (New Chatmanagerlistener () {

@Override

public void chatcreated (chat chat, booleancreatedlocally) {

if (!createdlocally)

Chat.addmessagelistener (Newmynewmessagelistener ());

}

});

In addition to thread-based chat messages, there are some clients that do not send thread IDs as part of chat. To handle this situation, smack attempts to match the received message to the closest match to the existing chat based on Jid. It tries to find chat with the full jid, and if it doesn't, try using the basic Jid to find chat. If no existing chat is found to match, a new chat is created.


XMPP Client library Smack development of 4.0.6 version five

Related Article

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.