Skip to content

vinodsral/wasync

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wAsync: A WebSockets/HTTP Client Library for Asynchronous Communication

wAsync is a Java based library allowing asynchronous communication with any WebServer supporting the WebSocket or Http Protocol. You can browser the javadoc As simple as

        Client client = ClientFactory.getDefault().newclient();

        RequestBuilder request = client.newRequestBuilder()
                .method(Request.METHOD.GET)
                .uri("http://wordnik.com")
                .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(WEBSOCKET)                        // Try WebSocket
                .transport(LONG_POLLING);                    // Fallback to Long-Polling

        Socket socket = client.create();
        socket.on(new Function<Reader>() {
            @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")
            .fire("bong");

By default, the FunctionResolver will associate the Decoder's type will be used to invoke the appropriate Function, if defined. For example,

   Decoder<String, POJO> d = new Decoder<String, POJO>() {
             @Override
             public POJO decode(String s) {
                 return new POJO(s);
             }
         }

will be associated to

   Function<String> f = new Function<POJO>() {
             @Override
             public void on(POJO t) {

             }
        }

You can also implement your own FunctionResolver to associate the Function with Decoder

         Socket socket = client.create();
         socket.on("myEvent", new Function<Reader>() { ...}

where myEvent could be read from the response's body.

You can download the jar or use Maven

          <dependency>
              <groupId>org.atmosphere</groupId>
              <artifactId>wasync</artifactId>
              <version>1.0.0.beta1</version>
          </dependency>

About

A Java Client for Writing WebSocket/Comet Application with the Atmopshere Framework

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.5%
  • Other 0.5%