public class WebSocketClient
extends java.lang.Object
WebSocketClient
allows to create multiple connections to multiple destinations
that can speak the websocket protocol.
When creating websocket connections, WebSocketClient
accepts a WebSocket
object (to receive events from the server), and returns a WebSocket.Connection
to
send data to the server.
Example usage is as follows:
WebSocketClientFactory factory = new WebSocketClientFactory(); factory.start(); WebSocketClient client = factory.newWebSocketClient(); // Configure the client WebSocket.Connection connection = client.open(new URI("ws://127.0.0.1:8080/"), new WebSocket.OnTextMessage() { public void onOpen(Connection connection) { // open notification } public void onClose(int closeCode, String message) { // close notification } public void onMessage(String data) { // handle incoming message } }).get(5, TimeUnit.SECONDS); connection.sendMessage("Hello World");
Constructor and Description |
---|
WebSocketClient()
Deprecated.
|
WebSocketClient(WebSocketClientFactory factory)
Creates a WebSocketClient with shared WebSocketClientFactory.
|
Modifier and Type | Method and Description |
---|---|
java.net.SocketAddress |
getBindAddress() |
java.util.Map<java.lang.String,java.lang.String> |
getCookies()
Returns the map of the cookies that are sent during the initial HTTP handshake
that upgrades to the websocket protocol.
|
java.util.List<java.lang.String> |
getExtensions() |
WebSocketClientFactory |
getFactory() |
MaskGen |
getMaskGen() |
int |
getMaxBinaryMessageSize() |
int |
getMaxIdleTime() |
int |
getMaxTextMessageSize() |
java.lang.String |
getOrigin() |
java.lang.String |
getProtocol() |
java.util.concurrent.Future<WebSocket.Connection> |
open(java.net.URI uri,
WebSocket websocket)
Asynchronously opens a websocket connection and returns a
Future to obtain the connection. |
WebSocket.Connection |
open(java.net.URI uri,
WebSocket websocket,
long maxConnectTime,
java.util.concurrent.TimeUnit units)
Opens a websocket connection to the URI and blocks until the connection is accepted or there is an error.
|
void |
setBindAddress(java.net.SocketAddress bindAddress) |
void |
setMaskGen(MaskGen maskGen) |
void |
setMaxBinaryMessageSize(int maxBinaryMessageSize)
Set the initial maximum binary message size for a connection.
|
void |
setMaxIdleTime(int maxIdleTime) |
void |
setMaxTextMessageSize(int maxTextMessageSize)
Set the initial maximum text message size for a connection.
|
void |
setOrigin(java.lang.String origin) |
void |
setProtocol(java.lang.String protocol) |
static java.net.InetSocketAddress |
toSocketAddress(java.net.URI uri) |
@Deprecated public WebSocketClient() throws java.lang.Exception
WebSocketClientFactory.newWebSocketClient()
Creates a WebSocketClient from a private WebSocketClientFactory.
This can be wasteful of resources if many clients are created.
java.lang.Exception
- if the private WebSocketClientFactory fails to startpublic WebSocketClient(WebSocketClientFactory factory)
Creates a WebSocketClient with shared WebSocketClientFactory.
factory
- the shared WebSocketClientFactory
public WebSocketClientFactory getFactory()
public java.net.SocketAddress getBindAddress()
setBindAddress(SocketAddress)
public void setBindAddress(java.net.SocketAddress bindAddress)
bindAddress
- the address to bind the socket channel togetBindAddress()
public int getMaxIdleTime()
WebSocketClientFactory.getSelectorManager()
is used.setMaxIdleTime(int)
public void setMaxIdleTime(int maxIdleTime)
maxIdleTime
- The max idle time in ms for connections opened by this clientgetMaxIdleTime()
public java.lang.String getProtocol()
setProtocol(String)
public void setProtocol(java.lang.String protocol)
protocol
- The subprotocol string for connections opened by this client.getProtocol()
public java.lang.String getOrigin()
setOrigin(String)
public void setOrigin(java.lang.String origin)
origin
- The origin URI of the client (eg "http://example.com")getOrigin()
public java.util.Map<java.lang.String,java.lang.String> getCookies()
Returns the map of the cookies that are sent during the initial HTTP handshake that upgrades to the websocket protocol.
public java.util.List<java.lang.String> getExtensions()
public MaskGen getMaskGen()
setMaskGen(MaskGen)
public void setMaskGen(MaskGen maskGen)
maskGen
- the mask generator to use, or null if not mask generator should be usedgetMaskGen()
public int getMaxTextMessageSize()
public void setMaxTextMessageSize(int maxTextMessageSize)
WebSocket.Connection.setMaxTextMessageSize(int)
.maxTextMessageSize
- The default maximum text message size (in characters) for a connectionpublic int getMaxBinaryMessageSize()
public void setMaxBinaryMessageSize(int maxBinaryMessageSize)
WebSocket.Connection.setMaxBinaryMessageSize(int)
.maxBinaryMessageSize
- The default maximum binary message size (in bytes) for a connectionpublic WebSocket.Connection open(java.net.URI uri, WebSocket websocket, long maxConnectTime, java.util.concurrent.TimeUnit units) throws java.io.IOException, java.lang.InterruptedException, java.util.concurrent.TimeoutException
Opens a websocket connection to the URI and blocks until the connection is accepted or there is an error.
uri
- The URI to connect to.websocket
- The WebSocket
instance to handle incoming events.maxConnectTime
- The interval to wait for a successful connectionunits
- the units of the maxConnectTimeWebSocket.Connection
java.io.IOException
- if the connection failsjava.lang.InterruptedException
- if the thread is interruptedjava.util.concurrent.TimeoutException
- if the timeout elapses before the connection is completedopen(URI, WebSocket)
public java.util.concurrent.Future<WebSocket.Connection> open(java.net.URI uri, WebSocket websocket) throws java.io.IOException
Asynchronously opens a websocket connection and returns a Future
to obtain the connection.
The caller must call Future.get(long, TimeUnit)
if they wish to impose a connect timeout on the open.
uri
- The URI to connect to.websocket
- The WebSocket
instance to handle incoming events.Future
to the WebSocket.Connection
java.io.IOException
- if the connection failsopen(URI, WebSocket, long, TimeUnit)
public static java.net.InetSocketAddress toSocketAddress(java.net.URI uri)
Copyright © 2012. All Rights Reserved.