Skip to content

WebSockets with fallback transports client library for Node.js, Android and Java

Notifications You must be signed in to change notification settings

libofeng/wasync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

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. 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>1.4.0</version> </dependency> 

As simple as

Clientclient = ClientFactory.getDefault().newClient(); RequestBuilderrequest = client.newRequestBuilder() .method(Request.METHOD.GET) .uri("http://async-io.org") .encoder(newEncoder<String, Reader>(){// Stream the request body@OverridepublicReaderencode(Strings){returnnewStringReader(s)} }) .decoder(newDecoder<String, Reader>(){@OverridepublicReaderdecode(Eventtype, Strings){returnnewStringReader(s)} }) .transport(Request.TRANSPORT.WEBSOCKET) // Try WebSocket .transport(Request.TRANSPORT.LONG_POLLING); // Fallback to Long-PollingSocketsocket = client.create(); socket.on(newFunction<Reader>(){@Overridepublicvoidon(Readerr){// Read the response } }).on(newFunction<IOException>(){@Overridepublicvoidon(Throwablet){// Some IOException occurred } }).open(request.build()) .fire("echo") .fire("bong");

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

Socketsocket = client.create(); socket.on(Event.CLOSE.name(), newFunction<String>(){@Overridepublicvoidon(Stringt){} }).on(Event.REOPENED.name(), newFunction<String>(){@Overridepublicvoidon(Stringt){} }).on(newFunction<IOException>(){@Overridepublicvoidon(IOExceptionioe){ioe.printStackTrace()} }).on(Event.OPEN.name(), newFunction<String>(){@Overridepublicvoidon(Stringt){} }).open(request.build());

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

AtmosphereClientclient = ClientFactory.getDefault().newClient(AtmosphereClient.class); RequestBuilderrequest = 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

SerializedClientclient = ClientFactory.getDefault().newClient(SerializedClient.class); SerializedOptionsBuilderb = client.newOptionsBuilder(); b.serializedFireStage(newDefaultSerializedFireStage()); RequestBuilderrequest = client.newRequestBuilder() .method(Request.METHOD.GET) .uri(targetUrl + "/suspend") .transport(Request.TRANSPORT.WEBSOCKET); Socketsocket = 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 = newDecoder<String, POJO>(){@OverridepublicPOJOdecode(Eventtype, Strings){if (type.equals(Event.MESSAGE)){returnnewPOJO(s)} else{returns} } }

will be associated to

Function<String> f = newFunction<POJO>(){@Overridepublicvoidon(POJOt){} }

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

Socketsocket = client.create(); socket.on("myEvent", newFunction<Reader>(){...}

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

Want to write an Android Client? See

Build Status

Build Status

githalytics.com alpha

About

WebSockets with fallback transports client library for Node.js, Android and Java

Resources

Stars

Watchers

Forks

Packages

No packages published