org.atmosphere.wasync
Interface Socket

All Known Implementing Classes:
DefaultSocket

public interface Socket

A Socket represent a connection to a remote server. A Socket abstract the transport used, the client will negotiate the best Request.transport() to communicate with the remote Server. 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");
 

Author:
Jeanfrancois Arcand

Nested Class Summary
static class Socket.EVENT
           
 
Method Summary
 void close()
          Close this Socket
 Future fire(Object data)
          Send data to the remote Server.
 Socket on(Function<? extends Object> function)
          Associate a Function with the Socket.
 Socket on(String functionMessage, Function<? extends Object> 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, TimeUnit tu)
          Connect to the remote Server using the Request's information, will timeout if the connection failed to open within a certain time
 

Method Detail

fire

Future fire(Object data)
            throws IOException
Send data to the remote Server.

Parameters:
data -
Returns:
a Future
Throws:
IOException

on

Socket on(Function<? extends Object> function)
Associate a 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

Parameters:
function - a Function
Returns:
this

on

Socket on(String functionMessage,
          Function<? extends Object> function)
Associate a 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 Function.MESSAGE but handling of custom message can be done using a FunctionResolver

Parameters:
function - a Function
Returns:
this

open

Socket open(Request request)
            throws IOException
Connect to the remote Server using the Request's information.

Parameters:
request - a Request
Returns:
this
Throws:
IOException

open

Socket open(Request request,
            long timeout,
            TimeUnit tu)
            throws IOException
Connect to the remote Server using the Request's information, will timeout if the connection failed to open within a certain time

Parameters:
request - a Request
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
this
Throws:
IOException

close

void close()
Close this Socket



Copyright © 2013. All Rights Reserved.