mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-23 04:57:58 +01:00
Updated for new API. Thanks to Michael Hunter.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7042 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
a5a6697496
commit
85dcb0a5c4
1 changed files with 44 additions and 30 deletions
|
@ -15,7 +15,8 @@ Messaging using Chats
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Sending messages back and forth is at the core of instant messaging. Although individual messages
|
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
|
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.
|
as a chat using the <tt>org.jivesoftware.smack.Chat</tt> class.
|
||||||
</p>
|
</p>
|
||||||
|
@ -28,33 +29,44 @@ A chat creates a new thread of messages (using a thread ID) between two users. T
|
||||||
following code snippet demonstrates how to create a new Chat with a user and then send
|
following code snippet demonstrates how to create a new Chat with a user and then send
|
||||||
them a text message:<p>
|
them a text message:<p>
|
||||||
|
|
||||||
<div class="code"><pre>
|
<div class="code"><pre><font color="gray"><i>// Assume we've created an XMPPConnection name
|
||||||
<font color="gray"><i>// Assume we've created an XMPPConnection name "connection".</i></font>
|
"connection".</i></font>
|
||||||
Chat newChat = connection.createChat(<font color="green">"jsmith@jivesoftware.com"</font>, new MessageListener() {
|
ChatManager chatmanager = connection.getChatManager();<br>Chat newChat = chatmanager.createChat(<font
|
||||||
|
color="green">"jsmith@jivesoftware.com"</font>, new MessageListener() {
|
||||||
|
|
||||||
public void processMessage(Chat chat, Message message) {
|
public void processMessage(Chat chat, Message message) {
|
||||||
System.out.println(<font color="green">"Received message: "</font> + message);
|
System.out.println(<font color="green">"Received message: "</font> + message);
|
||||||
}
|
}
|
||||||
});
|
}); <br>try {
|
||||||
chat.sendMessage(<font color="green">"Howdy!"</font>);
|
chat.sendMessage(<font color="green">"Howdy!"</font>); <br>} catch (XMPPException e) {<br> System.out.println("Error Delivering block");<br>}<br><br>
|
||||||
</pre></div><p>
|
</pre>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
|
||||||
The <tt>Chat.sendMessage(String)</tt> method is a convenience method that creates a Message
|
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
|
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
|
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
|
<tt>Chat.createMessage()</tt> and <tt>Chat.sendMessage(Message)</tt> methods, as in the
|
||||||
following code snippet:<p>
|
following code snippet:
|
||||||
|
|
||||||
|
<p>
|
||||||
|
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
Message newMessage = newChat.createMessage();
|
Message newMessage = newChat.createMessage();
|
||||||
newMessage.setBody(<font color="green">"Howdy!"</font>);
|
newMessage.setBody(<font color="green">"Howdy!"</font>);
|
||||||
message.setProperty(<font color="green">"favoriteColor"</font>, <font color="green">"red"</font>);
|
message.setProperty(<font color="green">"favoriteColor"</font>, <font color="green">"red"</font>);
|
||||||
newChat.sendMessage(newMessage);
|
newChat.sendMessage(newMessage);
|
||||||
</pre></div><p>
|
</pre>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
|
||||||
You'll also notice in the example above that we specified a MessageListener when creating a chat.
|
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 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>
|
The following code snippet uses the listener as a parrot-bot -- it echoes back everything the
|
||||||
|
other user types.
|
||||||
|
|
||||||
|
<p>
|
||||||
|
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
<font color="gray"><i> // Assume a MessageListener we've setup with a chat.</i></font>
|
<font color="gray"><i> // Assume a MessageListener we've setup with a chat.</i></font>
|
||||||
|
@ -63,7 +75,9 @@ The following code snippet uses the listener as a parrot-bot -- it echoes back e
|
||||||
<font color="gray"><i>// Send back the same text the other user sent us.</i></font>
|
<font color="gray"><i>// Send back the same text the other user sent us.</i></font>
|
||||||
chat.sendMessage(message.getBody());
|
chat.sendMessage(message.getBody());
|
||||||
}
|
}
|
||||||
</pre></div><p>
|
</pre>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
|
||||||
<br clear="all"/><br><br>
|
<br clear="all"/><br><br>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue