1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-23 06:42:05 +01:00
Smack/documentation/messaging.html
Matt Tucker c5ab2829d4 Doc fixes (SMACK-277)
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@8428 b35dd754-fafc-0310-a699-88a17e54d16e
2007-06-01 21:42:38 +00:00

94 lines
2.8 KiB
HTML

<html>
<head>
<title>Smack: Chat - Jive Software</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div class="header">
Messaging using Chats
</div>
<div class="nav">
&laquo; <a href="index.html">Table of Contents</a>
</div>
<p>
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.
</p>
<p class="subheader">
Chat
</p>
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:<p>
<div class="code"><pre><font color="gray"><i>// Assume we've created an XMPPConnection name "connection".</i></font>
ChatManager chatmanager = connection.getChatManager();
Chat newChat = chatmanager.createChat(<font
color="green">"jsmith@jivesoftware.com"</font>, new MessageListener() {
public void processMessage(Chat chat, Message message) {
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>);
}
</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:
<p>
<div class="code"><pre>
Message newMessage = new Message();
newMessage.setBody(<font color="green">"Howdy!"</font>);
message.setProperty(<font color="green">"favoriteColor"</font>, <font color="green">"red"</font>);
newChat.sendMessage(newMessage);
</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.
<p>
<div class="code"><pre>
<font color="gray"><i> // Assume a MessageListener we've setup with a chat.</i></font>
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());
}
</pre>
</div>
<p>
<br clear="all"/><br><br>
<div class="footer">
Copyright &copy; Jive Software 2002-2007
</div>
</body>
</html>