mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-22 20:12:07 +01:00
Doc updates.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6950 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
06a61c54dc
commit
e1b93839d7
3 changed files with 33 additions and 71 deletions
|
@ -23,34 +23,33 @@ important classes and concepts.
|
||||||
Requirements
|
Requirements
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
The only requirement for Smack is JDK 1.2 or later<sup>
|
The only requirement for Smack is JDK 1.5 or later. An XML parser is embedded in the
|
||||||
<a style="text-decoration:none;" href="#ssenote">1</a></sup>.
|
smack.jar file and no other third party libraries are required.<p>
|
||||||
An XML parser is embedded in the smack.jar file and no other third party
|
|
||||||
libraries are required.<p>
|
|
||||||
|
|
||||||
<sup>1</sup> <font size="-1"><i>JDK 1.2 and 1.3 users that wish to use SSL connections must have the
|
|
||||||
<a href="http://java.sun.com/products/jsse/index-103.html">JSSE</a> library in their classpath.</i></font>
|
|
||||||
|
|
||||||
<p class="subheader">
|
<p class="subheader">
|
||||||
Establishing a Connection
|
Establishing a Connection
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
The <tt>XMPPConnection</tt> class is used to create a connection to an
|
The <tt>XMPPConnection</tt> class is used to create a connection to an
|
||||||
XMPP server. To create an SSL connection, use the SSLXMPPConnection class.
|
XMPP server. Below are code examples for making a connection:<p>
|
||||||
Below are code examples for making a connection:<p>
|
|
||||||
|
|
||||||
<div class="code">
|
<div class="code">
|
||||||
<pre>
|
<pre>
|
||||||
<font color="gray"><i>// Create a connection to the jabber.org server.</i></font>
|
<font color="gray"><i>// Create a connection to the jabber.org server.</i></font>
|
||||||
XMPPConnection conn1 = <font color="navy"><b>new</b></font> XMPPConnection(<font color="green">"jabber.org"</font>);
|
XMPPConnection conn1 = <font color="navy"><b>new</b></font> XMPPConnection(<font color="green">"jabber.org"</font>);
|
||||||
|
conn1.connect();
|
||||||
|
|
||||||
<font color="gray"><i>// Create a connection to the jabber.org server on a specific port.</i></font>
|
<font color="gray"><i>// Create a connection to the jabber.org server on a specific port.</i></font>
|
||||||
XMPPConnection conn2 = <font color="navy"><b>new</b></font> XMPPConnection(<font color="green">"jabber.org"</font>, 5222);
|
ConnectionConfiguration config = new ConnectionConfiguration(<font color="green">"jabber.org"</font>, 5222);
|
||||||
|
XMPPConnection conn2 = <font color="navy"><b>new</b></font> XMPPConnection(config);
|
||||||
<font color="gray"><i>// Create an SSL connection to jabber.org.</i></font>
|
conn2.connect();
|
||||||
XMPPConnection connection = <font color="navy"><b>new</b></font> SSLXMPPConnection(<font color="green">"jabber.org"</font>);
|
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
|
<p>Note that maximum security will be used when connecting to the server by default (and when possible),
|
||||||
|
including use of TLS encryption. The ConnectionConfiguration class provides advanced control
|
||||||
|
over the connection created, such as the ability to disable or require encryption.</p>
|
||||||
|
|
||||||
<p>Once you've created a connection, you should login using a username and password
|
<p>Once you've created a connection, you should login using a username and password
|
||||||
with the <tt>XMPPConnection.login(String username, String password)</tt> method.
|
with the <tt>XMPPConnection.login(String username, String password)</tt> method.
|
||||||
Once you've logged in, you can being chatting with other users by creating
|
Once you've logged in, you can being chatting with other users by creating
|
||||||
|
|
|
@ -6,11 +6,7 @@
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<table border="0"><tr><td>
|
|
||||||
<img src="images/smacklogo.png" width="100" height="100" alt="Smack" border="0" hspace="10">
|
|
||||||
</td><td>
|
|
||||||
<font size="4"><b>Smack Documentation</b></font>
|
<font size="4"><b>Smack Documentation</b></font>
|
||||||
</div></td></tr></table>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<strong>Contents:</strong>
|
<strong>Contents:</strong>
|
||||||
|
@ -31,7 +27,7 @@
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<div class="footer">
|
<div class="footer">
|
||||||
Copyright © Jive Software 2002-2006
|
Copyright © Jive Software 2002-2007
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div class="header">
|
<div class="header">
|
||||||
Messaging using Chat and GroupChat
|
Messaging using Chats
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="nav">
|
<div class="nav">
|
||||||
|
@ -15,16 +15,9 @@ Messaging using Chat and GroupChat
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Sending messages back and forth is at the core of instant messaging. Two classes
|
Sending messages back and forth is at the core of instant messaging. Although individual messages
|
||||||
aid in sending and receiving messages:
|
can be sent and received as packets, it's generally easier to treat the string of messages
|
||||||
<ul>
|
as a chat using the <tt>org.jivesoftware.smack.Chat</tt> class.
|
||||||
<li> <tt>org.jivesoftware.smack.Chat</tt> -- used to send messages between two people.
|
|
||||||
<li> <tt>org.jivesoftware.smack.GroupChat</tt> -- used to join a chat room to send messages between many people.
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
Both the Chat and GroupChat classes use the <tt>org.jivesoftware.smack.packet.Message</tt> packet
|
|
||||||
class to send messages. In certain circumstances, you may wish to bypass the higher-level
|
|
||||||
Chat and GroupChat classes to send and listen for messages directly.
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class="subheader">
|
<p class="subheader">
|
||||||
|
@ -37,8 +30,13 @@ them a text message:<p>
|
||||||
|
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
<font color="gray"><i>// Assume we've created an XMPPConnection name "connection".</i></font>
|
<font color="gray"><i>// Assume we've created an XMPPConnection name "connection".</i></font>
|
||||||
Chat newChat = connection.createChat(<font color="green">"jsmith@jivesoftware.com"</font>);
|
Chat newChat = connection.createChat(<font color="green">"jsmith@jivesoftware.com"</font>, new MessageListener() {
|
||||||
newChat.sendMessage(<font color="green">"Howdy!"</font>);
|
|
||||||
|
public void processMessage(Chat chat, Message message) {
|
||||||
|
System.out.println(<font color="green">"Received message: "</font> + message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
chat.sendMessage(<font color="green">"Howdy!"</font>);
|
||||||
</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
|
||||||
|
@ -48,60 +46,29 @@ that you wish to set additional values on a Message before sending it, use the
|
||||||
following code snippet:<p>
|
following code snippet:<p>
|
||||||
|
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
<font color="gray"><i>// Assume we've created an XMPPConnection name "connection".</i></font>
|
|
||||||
Chat newChat = connection.createChat(<font color="green">"jsmith@jivesoftware.com"</font>);
|
|
||||||
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>
|
||||||
|
|
||||||
The Chat object allows you to easily listen for replies from the other chat participant.
|
You'll also notice in the example above that we specified a MessageListener when creating a chat.
|
||||||
The following code snippet is a parrot-bot -- it echoes back everything the other user types.<p>
|
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>
|
<div class="code"><pre>
|
||||||
<font color="gray"><i>// Assume we've created an XMPPConnection name "connection".</i></font>
|
<font color="gray"><i> // Assume a MessageListener we've setup with a chat.</i></font>
|
||||||
Chat newChat = connection.createChat(<font color="green">"jsmith@jivesoftware.com"</font>);
|
|
||||||
newMessage.setBody(<font color="green">"Hi, I'm an annoying parrot-bot! Type something back to me."</font>);
|
public void processMessage(Chat chat, Message message) {
|
||||||
<b>while</b> (<b>true</b>) {
|
<font color="gray"><i>// Send back the same text the other user sent us.</i></font>
|
||||||
<font color="gray"><i>// Wait for the next message the user types to us.</i></font>
|
chat.sendMessage(message.getBody());
|
||||||
Message message = newChat.nextMessage();
|
}
|
||||||
<font color="gray"><i>// Send back the same text the other user sent us.</i></font>
|
|
||||||
newChat.sendMessage(message.getBody());
|
|
||||||
}
|
|
||||||
</pre></div><p>
|
</pre></div><p>
|
||||||
|
|
||||||
The code above uses the <tt>Chat.nextMessage()</tt> method to get the next message, which
|
|
||||||
will wait indefinitely until another message comes in. There are other methods to wait
|
|
||||||
a specific amount of time for a new message, or you can add a listener that will be notified
|
|
||||||
every time a new message arrives.
|
|
||||||
|
|
||||||
<p class="subheader">
|
|
||||||
GroupChat
|
|
||||||
</p>
|
|
||||||
|
|
||||||
A group chat connects to a chat room on a server and allows you to send and receive messages
|
|
||||||
from a group of people. Before you can send or receive messages, you must join the room using
|
|
||||||
a nickname. The following code snippet connects to a chat room and sends a
|
|
||||||
message.<p>
|
|
||||||
|
|
||||||
<div class="code"><pre>
|
|
||||||
<font color="gray"><i>// Assume we've created an XMPPConnection name "connection".</i></font>
|
|
||||||
GroupChat newGroupChat = connection.createGroupChat(<font color="green">"test@jivesoftware.com"</font>);
|
|
||||||
<font color="gray"><i>// Join the group chat using the nickname "jsmith".</i></font>
|
|
||||||
newGroupChat.join(<font color="green">"jsmith"</font>);
|
|
||||||
<font color="gray"><i>// Send a message to all the other people in the chat room.</i></font>
|
|
||||||
newGroupChat.sendMessage(<font color="green">"Howdy!"</font>);
|
|
||||||
</pre></div><p>
|
|
||||||
|
|
||||||
In general, sending and receiving messages in a group chat works very similarly to
|
|
||||||
the <tt>Chat</tt> class. Method are also provided to get the list of the other
|
|
||||||
users in the room.<p>
|
|
||||||
|
|
||||||
<br clear="all" /><br><br>
|
<br clear="all" /><br><br>
|
||||||
|
|
||||||
<div class="footer">
|
<div class="footer">
|
||||||
Copyright © Jive Software 2002-2005
|
Copyright © Jive Software 2002-2007
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
Loading…
Reference in a new issue