2014-08-16 00:09:55 +02:00
|
|
|
Messaging using Chats
|
|
|
|
=====================
|
|
|
|
|
2015-03-07 21:05:31 +01:00
|
|
|
[Back](index.md)
|
2014-08-16 00:09:55 +02:00
|
|
|
|
|
|
|
Sending messages back and forth is at the core of instant messaging. Although
|
|
|
|
individual messages can be sent and received as packets, it's generally easier
|
|
|
|
to treat the string of messages as a chat using the
|
2017-01-11 19:35:55 +01:00
|
|
|
`org.jivesoftware.smack.chat2.Chat` class.
|
2014-08-16 00:09:55 +02:00
|
|
|
|
|
|
|
Chat
|
|
|
|
----
|
|
|
|
|
|
|
|
A chat creates a new thread of messages (using a thread ID) between two users.
|
|
|
|
The following code snippet demonstrates how to create a new Chat with a user
|
|
|
|
and then send them a text message:
|
|
|
|
|
|
|
|
```
|
2015-02-12 12:13:19 +01:00
|
|
|
// Assume we've created an XMPPConnection name "connection"._
|
2017-01-11 19:35:55 +01:00
|
|
|
ChatManager chatManager = ChatManager.getInstanceFor(connection);
|
|
|
|
chatManager.addListener(new IncomingChatMessageListener() {
|
|
|
|
@Override
|
|
|
|
void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
|
|
|
|
System.out.println("New message from " + from ": " + message.getBody());
|
|
|
|
}
|
2014-08-16 00:09:55 +02:00
|
|
|
});
|
2017-01-11 19:35:55 +01:00
|
|
|
EntityBareJid jid = JidCreate.entityBareFrom("jsmith@jivesoftware.com");
|
|
|
|
Chat chat = chatManager.chatWith(jid);
|
|
|
|
chat.sendMessage("Howdy!");
|
2014-08-16 00:09:55 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The `Chat.sendMessage(String)` method is a convenience method that creates a
|
|
|
|
Message object, sets the body using the String parameter, then sends the
|
|
|
|
message. In the case that you wish to set additional values on a Message
|
2017-01-11 19:35:55 +01:00
|
|
|
before sending it, use
|
|
|
|
`Chat.sendMessage(Message)` method, as in the following code snippet:
|
2014-08-16 00:09:55 +02:00
|
|
|
|
|
|
|
```
|
|
|
|
Message newMessage = new Message();
|
|
|
|
newMessage.setBody("Howdy!");
|
2016-06-13 14:15:01 +02:00
|
|
|
// Additional modifications to the message Stanza.
|
|
|
|
JivePropertiesManager.addProperty(newMessage, "favoriteColor", "red");
|
2017-01-11 19:35:55 +01:00
|
|
|
chat.sendMessage(newMessage);
|
2014-08-16 00:09:55 +02:00
|
|
|
```
|
|
|
|
|
2017-01-11 19:35:55 +01:00
|
|
|
You'll also notice in the example above that we specified an IncomingChatMessageListener.
|
|
|
|
The listener is notified any time a new chat message arrives.
|
|
|
|
The following code snippet uses the listener
|
2014-08-16 00:09:55 +02:00
|
|
|
as a parrot-bot -- it echoes back everything the other user types.
|
|
|
|
|
|
|
|
```
|
2017-01-11 19:35:55 +01:00
|
|
|
// Assume a IncomingChatMessageListener we've setup with a ChatManager
|
|
|
|
public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
|
|
|
|
// Send back the same text the other user sent us.
|
|
|
|
chat.sendMessage(message.getBody());
|
2014-08-16 00:09:55 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Copyright (C) Jive Software 2002-2008
|