public interface Socket
Request.TRANSPORT to communicate with the Server.
Depending on the transport used, one or two connections will be opened. For WebSocket, a single, bi-directional connection
will be used. For other transport like streaming, server side events and long-polling, a connection will be opened
to the server and will be suspended (stay opened) until an event happen on the server. A second connection will be opened every time the fire(Object)
method is invoked and cached for further re-use.
As simple as
Client client = AtmosphereClientFactory.getDefault().newClient();
RequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.uri(targetUrl + "/suspend")
.encoder(new Encoder<String, Reader>() { // Stream the request body
@Override
public Reader encode(String s) {
return new StringReader(s);
}
})
.decoder(new Decoder<String, Reader>() {
@Override
public Reader decode(String s) {
return new StringReader(s);
}
})
.transport(Request.TRANSPORT.WEBSOCKET) // Try WebSocket
.transport(Request.TRANSPORT.LONG_POLLING); // Fallback to Long-Polling
Socket socket = client.create();
socket.on("message", new Function<String>() {
@Override
public void on(Reader r) {
// Read the response
}
}).on(new Function<IOException>() {
@Override
public void on(Throwable t) {
// Some IOException occurred
}
}).open(request.build()).fire("echo");
| Modifier and Type | Interface and Description |
|---|---|
static class |
Socket.STATUS
The current state of the underlying Socket.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close this Socket, asynchronously.
|
Future |
fire(java.lang.Object data)
Send data to the remote Server.
|
Socket |
on(Event event,
Function<?> function)
|
Socket |
on(Function<?> function)
Associate a
Function with the Socket. |
Socket |
on(java.lang.String functionMessage,
Function<?> function)
Associate a
Function with the Socket. |
Socket |
open(Request request)
Connect to the remote Server using the
Request's information. |
Socket |
open(Request request,
long timeout,
java.util.concurrent.TimeUnit unit)
Connect to the remote Server using the
Request's information, will timeout if the connection failed to open
within a certain time. |
Socket.STATUS |
status()
Return the
Socket.STATUS of this Socket. |
Future fire(java.lang.Object data) throws java.io.IOException
Encoder, and then send to the server.
The server's response will be delivered to the set of defined Function using the opened Transport, e.g for
Request.TRANSPORT.WEBSOCKET, the same connection will be re-used and, for others transports, the suspended connection.data - object to sendFuturejava.io.IOExceptionSocket on(Function<?> function)
Function with the Socket. When a response is received, the library will try to associated
the decoded message (decoded by Decoder) to the defined type of the Functionfunction - a FunctionSocket on(java.lang.String functionMessage, Function<?> function)
Function with the Socket. When a response is received, the library will try to associated
the decoded message (decoded by Decoder) to the defined type of the Function. The default messages
are defined by Event but handling of custom message can be done using a FunctionResolverfunction - a FunctionSocket on(Event event, Function<?> function)
Function with an Event. When the event happen the library will try to associated
the decoded event (decoded by Decoder) to the defined type of the Function. The default event
are defined by Event but handling of custom event can be done using a FunctionResolverfunction - a FunctionSocket open(Request request) throws java.io.IOException
Request's information.request - a Requestjava.io.IOException - in case the connect fails or a network failure occurs.Socket open(Request request, long timeout, java.util.concurrent.TimeUnit unit) throws java.io.IOException
Request's information, will timeout if the connection failed to open
within a certain time.request - a Requesttimeout - the maximum time to waitunit - the time unit of the timeout argumentjava.io.IOException - in case the connect fails or a network failure occurs.void close()
Socket.STATUS status()
Socket.STATUS of this Socket.Copyright © 2019. All Rights Reserved.