Websocket ComponentAvailable as of Camel 2.10 The websocket component provides websocket endpoints for communicating with clients using websocket. The component uses Eclipse Jetty Server which implements the IETF specification (drafts and RFC 6455). It supports the protocols ws:// and wss://. To use wss:// protocol, the SSLContextParameters must be defined.
URI format
websocket://hostname[:port][/resourceUri][?options]
You can append query options to the URI in the following format, ?option=value&option=value&... Component OptionsThe WebsocketComponent can be configured prior to use, to setup host, to act as a websocket server.
Endpoint OptionsThe WebsocketEndpoint can be configured prior to use
Message HeadersThe websocket component uses 2 headers to indicate to either send messages back to a single/current client, or to all clients.
UsageIn 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 0.0.0.0:9292. // expose a echo websocket client, that sends back an echo from("websocket://echo") .log(">>> Message received from WebSocket Client : ${body}") .transform().simple("${body}${body}") // send back to the client, by sending the message to the same endpoint // this is needed as by default messages is InOnly // and we will by default send back to the current client using the provided connection key .to("websocket://echo"); This example is part of an unit test, which you can find here. As a client we use the AHC library which offers support for web socket as well. Here is another example where webapp resources location have been defined to allow the Jetty Application Server to not only register the WebSocket servlet but also to expose web resources for the browser. Resources should be defined under the webapp directory. from("activemq:topic:newsTopic") .routeId("fromJMStoWebSocket") .to("websocket://localhost:8443/newsTopic?sendToAll=true&staticResources=classpath:webapp"); Setting up SSL for WebSocket ComponentUsing the JSSE Configuration UtilityAs of Camel 2.10, the WebSocket component supports SSL/TLS configuration through the Camel JSSE Configuration Utility. This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels. The following examples demonstrate how to use the utility with the Cometd component. Programmatic configuration of the componentKeyStoreParameters ksp = new KeyStoreParameters(); ksp.setResource("/users/home/server/keystore.jks"); ksp.setPassword("keystorePassword"); KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); kmp.setKeyPassword("keyPassword"); TrustManagersParameters tmp = new TrustManagersParameters(); tmp.setKeyStore(ksp); SSLContextParameters scp = new SSLContextParameters(); scp.setKeyManagers(kmp); scp.setTrustManagers(tmp); CometdComponent commetdComponent = getContext().getComponent("cometds", CometdComponent.class); commetdComponent.setSslContextParameters(scp); Spring DSL based configuration of endpoint
...
<camel:sslContextParameters
id="sslContextParameters">
<camel:keyManagers
keyPassword="keyPassword">
<camel:keyStore
resource="/users/home/server/keystore.jks"
password="keystorePassword"/>
</camel:keyManagers>
<camel:trustManagers>
<camel:keyStore
resource="/users/home/server/keystore.jks"
password="keystorePassword"/>
</camel:trustManagers>
</camel:sslContextParameters>...
...
<to uri="websocket://127.0.0.1:8443/test?sslContextParametersRef=#sslContextParameters"/>...
Java DSL based configuration of endpoint
...
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() {
String uri = "websocket://127.0.0.1:8443/test?sslContextParametersRef=#sslContextParameters";
from(uri)
.log(">>> Message received from WebSocket Client : ${body}")
.to("mock:client")
.loop(10)
.setBody().constant(">> Welcome on board!")
.to(uri);
...
See Also
|