public class WebSocketClient extends 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 |
|---|---|
SocketAddress |
getBindAddress() |
Map<String,String> |
getCookies()
Returns the map of the cookies that are sent during the initial HTTP handshake
that upgrades to the websocket protocol.
|
List<String> |
getExtensions() |
WebSocketClientFactory |
getFactory() |
MaskGen |
getMaskGen() |
int |
getMaxBinaryMessageSize() |
int |
getMaxIdleTime() |
int |
getMaxTextMessageSize() |
String |
getOrigin() |
String |
getProtocol() |
Future<WebSocket.Connection> |
open(URI uri,
WebSocket websocket)
Asynchronously opens a websocket connection and returns a
Future to obtain the connection. |
WebSocket.Connection |
open(URI uri,
WebSocket websocket,
long maxConnectTime,
TimeUnit units)
Opens a websocket connection to the URI and blocks until the connection is accepted or there is an error.
|
void |
setBindAddress(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(String origin) |
void |
setProtocol(String protocol) |
static InetSocketAddress |
toSocketAddress(URI uri) |
@Deprecated public WebSocketClient() throws Exception
WebSocketClientFactory.newWebSocketClient()Creates a WebSocketClient from a private WebSocketClientFactory.
This can be wasteful of resources if many clients are created.
Exception - if the private WebSocketClientFactory fails to startpublic WebSocketClient(WebSocketClientFactory factory)
Creates a WebSocketClient with shared WebSocketClientFactory.
factory - the shared WebSocketClientFactorypublic WebSocketClientFactory getFactory()
public SocketAddress getBindAddress()
setBindAddress(SocketAddress)public void setBindAddress(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 String getProtocol()
setProtocol(String)public void setProtocol(String protocol)
protocol - The subprotocol string for connections opened by this client.getProtocol()public String getOrigin()
setOrigin(String)public void setOrigin(String origin)
origin - The origin URI of the client (eg "http://example.com")getOrigin()public Map<String,String> getCookies()
Returns the map of the cookies that are sent during the initial HTTP handshake that upgrades to the websocket protocol.
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(URI uri, WebSocket websocket, long maxConnectTime, TimeUnit units) throws IOException, InterruptedException, 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.ConnectionIOException - if the connection failsInterruptedException - if the thread is interruptedTimeoutException - if the timeout elapses before the connection is completedopen(URI, WebSocket)public Future<WebSocket.Connection> open(URI uri, WebSocket websocket) throws 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.ConnectionIOException - if the connection failsopen(URI, WebSocket, long, TimeUnit)public static InetSocketAddress toSocketAddress(URI uri)
Copyright © 1995-2015 Mort Bay Consulting. All Rights Reserved.