2003-06-19 20:11:37 +02:00
|
|
|
<html>
|
|
|
|
<head>
|
2007-02-08 07:12:01 +01:00
|
|
|
<title>Smack: Chat - Jive Software</title>
|
|
|
|
<link rel="stylesheet" type="text/css" href="style.css"/>
|
2003-06-19 20:11:37 +02:00
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div class="header">
|
2007-02-08 07:12:01 +01:00
|
|
|
Messaging using Chats
|
2003-06-19 20:11:37 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="nav">
|
2007-02-08 07:12:01 +01:00
|
|
|
« <a href="index.html">Table of Contents</a>
|
2003-06-19 20:11:37 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<p>
|
2007-02-08 07:12:01 +01: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 <tt>org.jivesoftware.smack.Chat</tt> class.
|
2003-06-19 20:11:37 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class="subheader">
|
2007-02-08 07:12:01 +01:00
|
|
|
Chat
|
2003-06-19 20:11:37 +02:00
|
|
|
</p>
|
|
|
|
|
2007-02-08 07:12:01 +01:00
|
|
|
A chat creates a new thread of messages (using a thread ID) between two users. The
|
2003-06-19 20:11:37 +02:00
|
|
|
following code snippet demonstrates how to create a new Chat with a user and then send
|
|
|
|
them a text message:<p>
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
<div class="code"><pre><font color="gray"><i>// Assume we've created a Connection name "connection".</i></font>
|
2007-06-01 23:42:38 +02:00
|
|
|
ChatManager chatmanager = connection.getChatManager();
|
|
|
|
Chat newChat = chatmanager.createChat(<font
|
2007-02-08 07:12:01 +01:00
|
|
|
color="green">"jsmith@jivesoftware.com"</font>, new MessageListener() {
|
2007-02-04 23:53:16 +01:00
|
|
|
public void processMessage(Chat chat, Message message) {
|
2007-06-01 23:42:38 +02:00
|
|
|
System.out.println(<font color="green">"Received message: "</font> + message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
newChat.sendMessage(<font color="green">"Howdy!"</font>);
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
System.out.println(<font color="green">"Error Delivering block"</font>);
|
|
|
|
}
|
2007-02-08 07:12:01 +01:00
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
<p>
|
|
|
|
|
|
|
|
The <tt>Chat.sendMessage(String)</tt> 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 before sending it, use the
|
|
|
|
<tt>Chat.createMessage()</tt> and <tt>Chat.sendMessage(Message)</tt> methods, as in the
|
|
|
|
following code snippet:
|
2003-06-19 20:11:37 +02:00
|
|
|
|
2007-02-08 07:12:01 +01:00
|
|
|
<p>
|
2003-06-19 20:11:37 +02:00
|
|
|
|
|
|
|
<div class="code"><pre>
|
2007-06-01 23:42:38 +02:00
|
|
|
Message newMessage = new Message();
|
2003-06-19 20:11:37 +02:00
|
|
|
newMessage.setBody(<font color="green">"Howdy!"</font>);
|
|
|
|
message.setProperty(<font color="green">"favoriteColor"</font>, <font color="green">"red"</font>);
|
|
|
|
newChat.sendMessage(newMessage);
|
2007-02-08 07:12:01 +01:00
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
<p>
|
|
|
|
|
|
|
|
You'll also notice in the example above that we specified a MessageListener when creating a
|
|
|
|
chat.
|
|
|
|
The listener is notified any time a new message arrives from the other user in the chat.
|
|
|
|
The following code snippet uses the listener as a parrot-bot -- it echoes back everything the
|
|
|
|
other user types.
|
2003-06-19 20:11:37 +02:00
|
|
|
|
2007-02-08 07:12:01 +01:00
|
|
|
<p>
|
2003-06-19 20:11:37 +02:00
|
|
|
|
|
|
|
<div class="code"><pre>
|
2007-02-08 07:12:01 +01:00
|
|
|
<font color="gray"><i> // Assume a MessageListener we've setup with a chat.</i></font>
|
2003-06-19 20:11:37 +02:00
|
|
|
|
2007-02-04 23:53:16 +01:00
|
|
|
public void processMessage(Chat chat, Message message) {
|
|
|
|
<font color="gray"><i>// Send back the same text the other user sent us.</i></font>
|
|
|
|
chat.sendMessage(message.getBody());
|
|
|
|
}
|
2007-02-08 07:12:01 +01:00
|
|
|
</pre>
|
|
|
|
</div>
|
2011-03-14 02:53:42 +01:00
|
|
|
|
|
|
|
<p class="subheader">
|
|
|
|
Incoming Chat
|
|
|
|
</p>
|
|
|
|
|
|
|
|
When chats are prompted by another user, the setup is slightly different since
|
|
|
|
you are receiving a chat message first. Instead of explicitly creating a chat to send
|
|
|
|
messages, you need to register to handle newly created Chat instances when the ChatManager
|
|
|
|
creates them.
|
|
|
|
</br>
|
|
|
|
</br>
|
|
|
|
The ChatManager will already find a matching chat (by thread id) and if none exists, then it
|
|
|
|
will create a new one that does match. To get this new chat, you have to register to be
|
|
|
|
notified when it happens. You can register a message listener to receive all future messages as
|
|
|
|
part of this handler.<p>
|
|
|
|
|
|
|
|
<div class="code"><pre><font color="gray"><i>// Assume we've created a Connection name "connection".</i></font>
|
|
|
|
ChatManager chatmanager = connection.getChatManager().addChatListener(
|
|
|
|
new ChatManagerListener() {
|
|
|
|
@Override
|
|
|
|
public void chatCreated(Chat chat, boolean createdLocally)
|
|
|
|
{
|
|
|
|
if (!createdLocally)
|
|
|
|
chat.addMessageListener(new MyNewMessageListener());;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
In addition to thread based chat messages, there are some clients that
|
|
|
|
do not send a thread id as part of the chat. To handle this scenario,
|
|
|
|
Smack will attempt match the incoming messages to the best fit existing
|
|
|
|
chat, based on the JID. It will attempt to find a chat with the same full
|
|
|
|
JID, failing that, it will try the base JID. If no existing chat to the
|
|
|
|
user can found, then a new one is created.
|
2007-02-08 07:12:01 +01:00
|
|
|
<p>
|
2003-06-19 20:11:37 +02:00
|
|
|
|
2007-02-08 07:12:01 +01:00
|
|
|
<br clear="all"/><br><br>
|
2003-06-19 20:11:37 +02:00
|
|
|
|
|
|
|
<div class="footer">
|
2008-11-03 17:28:57 +01:00
|
|
|
Copyright © Jive Software 2002-2008
|
2003-06-19 20:11:37 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|