public class Jsr356WebSocketClient extends java.lang.Object implements WebSocketClient
A connector is bound to its configuration specified
javax.websockets
WebSocket URI.
A single connector instance supports sinking at most one stream and sourcing at most one stream.
Sample use:
// assuming a properties file containing at least:
// ws.uri=ws://myWsServerHost/myService
String propsPath = <path to properties file>;
Properties properties = new Properties();
properties.load(Files.newBufferedReader(new File(propsPath).toPath()));
Topology t = ...;
Jsr356WebSocketClient wsclient = new Jsr356WebSocketClient(t, properties);
// send a stream's JsonObject tuples as JSON WebSocket text messages
TStream<JsonObject> s = ...;
wsclient.send(s);
// create a stream of JsonObject tuples from received JSON WebSocket text messages
TStream<JsonObject> r = wsclient.receive();
r.print();
Note, the WebSocket protocol differentiates between text/String and binary/byte messages. A receiver only receives the messages of the type that it requests.
The connector is written against the JSR356 javax.websockets
API.
javax.websockets
uses the ServiceLoader
to load
an implementation of javax.websocket.ContainerProvider
.
The supplied connectors/javax.websocket-client
provides one
such implementation. To use it, include
connectors/javax.websocket-client/lib/javax.websocket-client.jar
on your classpath.
Constructor and Description |
---|
Jsr356WebSocketClient(Topology t,
java.util.Properties config,
Supplier<javax.websocket.WebSocketContainer> containerFn)
Create a new Web Socket Client connector.
|
Jsr356WebSocketClient(Topology t,
java.util.Properties config)
Create a new Web Socket Client connector.
|
Modifier and Type | Method and Description |
---|---|
TStream<com.google.gson.JsonObject> |
receive()
Create a stream of JsonObject tuples from received JSON WebSocket text messages.
|
TStream<byte[]> |
receiveBytes()
Create a stream of byte[] tuples from received WebSocket binary messages.
|
TStream<java.lang.String> |
receiveString()
Create a stream of String tuples from received WebSocket text messages.
|
TSink<com.google.gson.JsonObject> |
send(TStream<com.google.gson.JsonObject> stream)
Send a stream's JsonObject tuples as JSON in a WebSocket text message.
|
TSink<byte[]> |
sendBytes(TStream<byte[]> stream)
Send a stream's byte[] tuples in a WebSocket binary message.
|
TSink<java.lang.String> |
sendString(TStream<java.lang.String> stream)
Send a stream's String tuples in a WebSocket text message.
|
Topology |
topology()
Topology this element is contained in.
|
public Jsr356WebSocketClient(Topology t, java.util.Properties config)
Configuration parameters:
config
are ignored.t
- the topology to add the connector toconfig
- the connector's configurationpublic Jsr356WebSocketClient(Topology t, java.util.Properties config, Supplier<javax.websocket.WebSocketContainer> containerFn)
This constructor is made available in case the container created
by Jsr356WebSocketClient(Topology, Properties)
lacks the configuration needed for a particular use case.
At topology runtime containerFn.get()
will be called to
get a javax.websocket.WebSocketContainer
that will be used to
connect to the WebSocket server.
Only the "ws.uri" config
parameter is used.
t
- the topology to add the connector toconfig
- the connector's configurationcontainerFn
- supplier for a WebSocketContainer
. May be null.public TSink<com.google.gson.JsonObject> send(TStream<com.google.gson.JsonObject> stream)
send
in interface WebSocketClient
stream
- the streampublic TSink<java.lang.String> sendString(TStream<java.lang.String> stream)
sendString
in interface WebSocketClient
stream
- the streampublic TSink<byte[]> sendBytes(TStream<byte[]> stream)
sendBytes
in interface WebSocketClient
stream
- the streampublic TStream<com.google.gson.JsonObject> receive()
receive
in interface WebSocketClient
public TStream<java.lang.String> receiveString()
Note, the WebSocket protocol differentiates between text/String and binary/byte messages. This method only receives messages sent as text.
receiveString
in interface WebSocketClient
public TStream<byte[]> receiveBytes()
Note, the WebSocket protocol differentiates between text/String and binary/byte messages. This method only receives messages sent as bytes.
receiveBytes
in interface WebSocketClient
public Topology topology()
TopologyElement
topology
in interface TopologyElement
Copyright IBM 2015,2016 - 2f6ad0e-20160307-0902