camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Websocket
Date Fri, 08 Jun 2012 09:30:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Websocket">Websocket</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (13)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >You can append query options to the
URI in the following format, {{?option=value&amp;option=value&amp;...}} <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >h3. <span class="diff-added-words"style="background-color:
#dfd;">Component</span> Options <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
{{WebsocketComponent}} can be configured prior to use, to setup host, to act as a websocket
server.  <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{div:class=confluenceTableSmall} <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">||
Name || Default Value || Description || <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">||
Option || Default || Description || <br>| host | 0.0.0.0 | The hostname. | <br>|
port | 9292 | The port number. | <br>| staticResources | {{null}} | Path for static
resources such as index.html files etc. If this option has been configured, then a server
is started on the given hostname and port, to service the static resources, eg such as an
index.html file. If this option has not been configured, then no server is started. | <br></td></tr>
            <tr><td class="diff-unchanged" >| sslContextParameters | | Reference
to a {{org.apache.camel.util.jsse.SSLContextParameters}} in the [Registry|http://camel.apache.org/registry.html].&amp;nbsp;
This reference overrides any configured SSLContextParameters at the component level.&amp;nbsp;
See [Using the JSSE Configuration Utility|CAMEL:Camel Configuration Utilities]. \\ | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{enableJmx}} | {{false}} | If this option is true, Jetty JMX support will be enabled for
this endpoint. See [Jetty JMX support|#Jetty JMX support] for more details. | <br>|
{{sslKeyPassword}} | {{null}} | *Consumer only*: The password for the keystore when using
SSL. | <br>| {{sslPassword}} | {{null}} | *Consumer only*: The password when using SSL.
| <br>| {{sslKeystore}} | {{null}} | *Consumer only*: The path to the keystore. | <br>|
{{minThreads}} | {{null}} | *Consumer only*: To set a value for minimum number of threads
in server thread pool. | <br>| {{maxThreads}} | {{null}} | *Consumer only*: To set a
value for maximum number of threads in server thread pool. | <br>| {{threadPool}} |
{{null}} | *Consumer only*: To use a custom thread pool for the server. | <br></td></tr>
            <tr><td class="diff-unchanged" >{div} <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">You
can append query options to the URI in the following format, {{?option=value&amp;option=value&amp;...}}
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Endpoint Options <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">Here
is some examples on How to pass the parameters <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
{{WebsocketEndpoint}} can be configured prior to use <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">To
use ssl and websocket secure \--&gt; websocket://localhost:8443?/test?sslContextParametersRef=#sslContextParameters
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{div:class=confluenceTableSmall}
<br>|| Option || Default || Description || <br>| sslContextParameters | | Reference
to a {{org.apache.camel.util.jsse.SSLContextParameters}} in the [Registry|http://camel.apache.org/registry.html].&amp;nbsp;
This reference overrides any configured SSLContextParameters at the component level.&amp;nbsp;
See [Using the JSSE Configuration Utility|CAMEL:Camel Configuration Utilities]. \\ | <br>|
sendToAll | {{null}} | *Producer only:* To send to all websocket subscribers. Can be used
to configure on endpoint level, instead of having to use the {{WebsocketConstants.SEND_TO_ALL}}
header on the message. | <br>{div} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Message Headers <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{div} <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h3.
Component Options <br> <br>The {{WebsocketComponent}} must be configured prior
to use, to setup host, to act as a websocket server.  <br> <br>{div:class=confluenceTableSmall}
<br>|| Option || Default || Description || <br>| Host | 0.0.0.0 | The hostname.
| <br>| Port | 9292 | The port number. | <br>| StaticResources | {{null}} | Path
for static resources such as index.html files etc. | <br>{div} <br> <br>h3.
Endpoint Options <br> <br>The {{WebsocketEndpoint}} can be configured prior to
use <br> <br>{div:class=confluenceTableSmall} <br>|| Option || Default ||
Description || <br>| sendToAll | {{null}} | To send to all websocket subscribers. Can
be used to configure on endpoint level, instead of having to use the {{WebsocketConstants.SEND_TO_ALL}}
header on the message. | <br>{div} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Usage <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br>-
[AHC] <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">-
[Jetty] <br>- [Twitter Websocket Example] demonstrates how to poll a constant feed of
twitter searches and publish results in real time using web socket to a web page. <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Websocket-WebsocketComponent"></a>Websocket Component</h2>
<p><b>Available as of Camel 2.10</b></p>

<p>The <b>websocket</b> component provides websocket <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">endpoints</a> for communicating with clients using websocket. The
component uses Eclipse Jetty Server which implements the <a href="http://tools.ietf.org/html/rfc6455"
class="external-link" rel="nofollow">IETF</a> specification (drafts and RFC 6455).
It supports the protocols ws:// and wss://. To use wss:// protocol, the SSLContextParameters
must be defined.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Version currently
supported</b><br />As Camel 2.10 uses Jetty 7.5.4.v20111024, only the D00 to <a
href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-13" class="external-link"
rel="nofollow">D13</a> IETF implementations are available.</td></tr></table></div>

<h3><a name="Websocket-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
websocket:<span class="code-comment">//hostname[:port][/resourceUri][?options]</span>
</pre>
</div></div>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="Websocket-ComponentOptions"></a>Component Options</h3>

<p>The <tt>WebsocketComponent</tt> can be configured prior to use, to setup
host, to act as a websocket server. </p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> host </td>
<td class='confluenceTd'> 0.0.0.0 </td>
<td class='confluenceTd'> The hostname. </td>
</tr>
<tr>
<td class='confluenceTd'> port </td>
<td class='confluenceTd'> 9292 </td>
<td class='confluenceTd'> The port number. </td>
</tr>
<tr>
<td class='confluenceTd'> staticResources </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Path for static resources such as index.html files etc. If
this option has been configured, then a server is started on the given hostname and port,
to service the static resources, eg such as an index.html file. If this option has not been
configured, then no server is started. </td>
</tr>
<tr>
<td class='confluenceTd'> sslContextParameters </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Reference to a <tt>org.apache.camel.util.jsse.SSLContextParameters</tt>
in the <a href="http://camel.apache.org/registry.html" class="external-link" rel="nofollow">Registry</a>.&nbsp;
This reference overrides any configured SSLContextParameters at the component level.&nbsp;
See <a href="/confluence/display/CAMEL/Camel+Configuration+Utilities" title="Camel Configuration
Utilities">Using the JSSE Configuration Utility</a>. <br class="atl-forced-newline"
/> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>enableJmx</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If this option is true, Jetty JMX support will be enabled
for this endpoint. See <a href="#Websocket-JettyJMXsupport">Jetty JMX support</a>
for more details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sslKeyPassword</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Consumer only</b>: The password for the keystore
when using SSL. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sslPassword</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Consumer only</b>: The password when using
SSL. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sslKeystore</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Consumer only</b>: The path to the keystore.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>minThreads</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Consumer only</b>: To set a value for minimum
number of threads in server thread pool. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxThreads</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Consumer only</b>: To set a value for maximum
number of threads in server thread pool. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>threadPool</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Consumer only</b>: To use a custom thread pool
for the server. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Websocket-EndpointOptions"></a>Endpoint Options</h3>

<p>The <tt>WebsocketEndpoint</tt> can be configured prior to use</p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> sslContextParameters </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Reference to a <tt>org.apache.camel.util.jsse.SSLContextParameters</tt>
in the <a href="http://camel.apache.org/registry.html" class="external-link" rel="nofollow">Registry</a>.&nbsp;
This reference overrides any configured SSLContextParameters at the component level.&nbsp;
See <a href="/confluence/display/CAMEL/Camel+Configuration+Utilities" title="Camel Configuration
Utilities">Using the JSSE Configuration Utility</a>. <br class="atl-forced-newline"
/> </td>
</tr>
<tr>
<td class='confluenceTd'> sendToAll </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Producer only:</b> To send to all websocket
subscribers. Can be used to configure on endpoint level, instead of having to use the <tt>WebsocketConstants.SEND_TO_ALL</tt>
header on the message. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Websocket-MessageHeaders"></a>Message Headers</h3>

<p>The websocket component uses 2 headers to indicate to either send messages back to
a single/current client, or to all clients.</p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Key</th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>WebsocketConstants.SEND_TO_ALL</tt> </td>
<td class='confluenceTd'> Sends the message to all clients which are currently connected.
You can use the <tt>sendToAll</tt> option on the endpoint instead of using this
header. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>WebsocketConstants.CONNECTION_KEY</tt> </td>
<td class='confluenceTd'> Sends the message to the client with the given connection
key. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Websocket-Usage"></a>Usage</h3>

<p>In this example we let Camel exposes a websocket server which clients can communicate
with. The websocket server uses the default host and port, which would be <tt>0.0.0.0:9292</tt>.<br/>
The example will send back an echo of the input. To send back a message, we need to send the
transformed message to the same endpoint <tt>"websocket://echo"</tt>. This is
needed<br/>
because by default the messaging is InOnly. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// expose a echo websocket client,
that sends back an echo
</span>from(<span class="code-quote">"websocket:<span class="code-comment">//echo"</span>)
</span>    .log(<span class="code-quote">"&gt;&gt;&gt; Message received
from WebSocket Client : ${body}"</span>)
    .transform().simple(<span class="code-quote">"${body}${body}"</span>)
    <span class="code-comment">// send back to the client, by sending the message to
the same endpoint
</span>    <span class="code-comment">// <span class="code-keyword">this</span>
is needed as by <span class="code-keyword">default</span> messages is InOnly
</span>    <span class="code-comment">// and we will by <span class="code-keyword">default</span>
send back to the current client using the provided connection key
</span>    .to(<span class="code-quote">"websocket:<span class="code-comment">//echo"</span>);</span>
</pre>
</div></div>

<p>This example is part of an unit test, which you can find <a href="https://svn.apache.org/repos/asf/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketRouteExampleTest.java"
class="external-link" rel="nofollow">here</a>. As a client we use the <a href="/confluence/display/CAMEL/AHC"
title="AHC">AHC</a> library which offers support for web socket as well. </p>

<h3><a name="Websocket-SettingupSSLforWebSocketComponent"></a>Setting up
SSL for WebSocket Component</h3>

<h4><a name="Websocket-UsingtheJSSEConfigurationUtility"></a>Using the JSSE
Configuration Utility</h4>

<p>As of Camel 2.10, the WebSocket component supports SSL/TLS configuration through
the <a href="/confluence/display/CAMEL/Camel+Configuration+Utilities" title="Camel Configuration
Utilities">Camel JSSE Configuration Utility</a>.&nbsp; This utility greatly decreases
the amount of component specific code you need to write and is configurable at the endpoint
and component levels.&nbsp; The following examples demonstrate how to use the utility
with the Cometd component.</p>

<h5><a name="Websocket-Programmaticconfigurationofthecomponent"></a>Programmatic
configuration of the component</h5>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
KeyStoreParameters ksp = <span class="code-keyword">new</span> KeyStoreParameters();
ksp.setResource(<span class="code-quote">"/users/home/server/keystore.jks"</span>);
ksp.setPassword(<span class="code-quote">"keystorePassword"</span>);

KeyManagersParameters kmp = <span class="code-keyword">new</span> KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword(<span class="code-quote">"keyPassword"</span>);

TrustManagersParameters tmp = <span class="code-keyword">new</span> TrustManagersParameters();
tmp.setKeyStore(ksp);

SSLContextParameters scp = <span class="code-keyword">new</span> SSLContextParameters();
scp.setKeyManagers(kmp);
scp.setTrustManagers(tmp);

CometdComponent commetdComponent = getContext().getComponent(<span class="code-quote">"cometds"</span>,
CometdComponent.class);
commetdComponent.setSslContextParameters(scp);
</pre>
</div></div>

<h5><a name="Websocket-SpringDSLbasedconfigurationofendpoint"></a>Spring
DSL based configuration of endpoint</h5>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
...
  &lt;camel:sslContextParameters
      id=<span class="code-quote">"sslContextParameters"</span>&gt;
    &lt;camel:keyManagers
        keyPassword=<span class="code-quote">"keyPassword"</span>&gt;
      &lt;camel:keyStore
          resource=<span class="code-quote">"/users/home/server/keystore.jks"</span>
          password=<span class="code-quote">"keystorePassword"</span>/&gt;
    <span class="code-tag">&lt;/camel:keyManagers&gt;</span>
    <span class="code-tag">&lt;camel:trustManagers&gt;</span>
      &lt;camel:keyStore
          resource=<span class="code-quote">"/users/home/server/keystore.jks"</span>
          password=<span class="code-quote">"keystorePassword"</span>/&gt;
    <span class="code-tag">&lt;/camel:trustManagers&gt;</span>
  <span class="code-tag">&lt;/camel:sslContextParameters&gt;</span>...
...
  <span class="code-tag">&lt;to uri=<span class="code-quote">"websocket://127.0.0.1:8443/test?sslContextParametersRef=#sslContextParameters"</span>/&gt;</span>...
</pre>
</div></div>

<h5><a name="Websocket-JavaDSLbasedconfigurationofendpoint"></a>Java DSL
based configuration of endpoint</h5>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
...
    <span class="code-keyword">protected</span> RouteBuilder createRouteBuilder()
<span class="code-keyword">throws</span> Exception {
        <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
            <span class="code-keyword">public</span> void configure() {
                
                <span class="code-object">String</span> uri = <span class="code-quote">"websocket:<span
class="code-comment">//127.0.0.1:8443/test?sslContextParametersRef=#sslContextParameters"</span>;
</span>                
                from(uri)
                     .log(<span class="code-quote">"&gt;&gt;&gt; Message
received from WebSocket Client : ${body}"</span>)
                     .to(<span class="code-quote">"mock:client"</span>)
                     .loop(10)
                         .setBody().constant(<span class="code-quote">"&gt;&gt;
Welcome on board!"</span>)
                         .to(uri);
...
</pre>
</div></div>

<h3><a name="Websocket-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/AHC" title="AHC">AHC</a></li>
	<li><a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a></li>
	<li><a href="/confluence/display/CAMEL/Twitter+Websocket+Example" title="Twitter
Websocket Example">Twitter Websocket Example</a> demonstrates how to poll a constant
feed of twitter searches and publish results in real time using web socket to a web page.</li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Websocket">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=27841331&revisedVersion=10&originalVersion=9">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Websocket?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message