Git Product home page Git Product logo

wasync's Introduction

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. wAsync can be used with Node.js, Android, Atmosphere or any WebSocket Framework. To get started, read this super simple Tutorial or read the FAQ

You can browse the javadoc or browse our samples.

You can download the jar or use Maven

          <dependency>
              <groupId>org.atmosphere</groupId>
              <artifactId>wasync</artifactId>
              <version>2.1.2</version>
          </dependency>

As simple as

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

        RequestBuilder request = client.newRequestBuilder()
                .method(Request.METHOD.GET)
                .uri("http://async-io.org")
                .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(Event type, 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(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");

Life cycle of the underlying Socket can easily be implemented as well

           Socket socket = client.create();
           socket.on(Event.CLOSE.name(), new Function<String>() {
               @Override
               public void on(String t) {
               }
           }).on(Event.REOPENED.name(), new Function<String>() {
               @Override
               public void on(String t) {
               }
           }).on(new Function<IOException>() {
               @Override
               public void on(IOException ioe) {
                   ioe.printStackTrace();
               }
           }).on(Event.OPEN.name(), new Function<String>() {
               @Override
               public void on(String t) {
               }
           }).open(request.build());

You can also use the specialized clients. For example, to transparently enable Atmosphere's Protocol

       AtmosphereClient client = ClientFactory.getDefault().newClient(AtmosphereClient.class);

       RequestBuilder request = client.newRequestBuilder()
    		   .method(Request.METHOD.GET)
    		   .uri(targetUrl + "/suspend")
               .trackMessageLength(true)
    		   .transport(Request.TRANSPORT.LONG_POLLING);

or if you want to serialize the fire() method call so events are asynchronously sent in the order the fire method is called

        SerializedClient client = ClientFactory.getDefault().newClient(SerializedClient.class);

        SerializedOptionsBuilder b = client.newOptionsBuilder();
        b.serializedFireStage(new DefaultSerializedFireStage());

        RequestBuilder request = client.newRequestBuilder()
                .method(Request.METHOD.GET)
                .uri(targetUrl + "/suspend")
                .transport(Request.TRANSPORT.WEBSOCKET);

        Socket socket = client.create(b.build());

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(Event type, String s) {
                 if (type.equals(Event.MESSAGE)) {
                    return new POJO(s);
                 } else {
                    return 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.

Want to write an Android Client? See

Build Status

Build Status

[Analytics]

wasync's People

Contributors

jfarcand avatar thabach avatar slovdahl avatar vinodsral avatar bartfaitamas avatar ricardojlrufino avatar ratsam avatar r-bhuvan avatar khernyo avatar pdegol avatar sakno avatar

Watchers

James Cloos avatar Thai Thinh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.