AutobahnAndroid


Source link: https://github.com/crossbario/autobahn-android

Autobahn|Java

Client library providing WAMP on Java 8 ( Netty) and Android, plus (secure) WebSocket for Android.

|


Autobahn|Java is a subproject of the Autobahn project and provides open-source client implementations for

running on Android and Netty/Java8/JVM.

The WebSocket layer is using a callback based user API, and is specifically written for Android. Eg it does not run any network stuff on the main (UI) thread.

The WAMP layer is using Java 8 CompletableFuture for WAMP actions (call, register, publish and subscribe) and the Observer pattern for WAMP session, subscription and registration lifecycle events.

The library is MIT licensed, maintained by the Crossbar.io Project, tested using the AutobahnTestsuite and published as a JAR to Maven and as a Docker toolchain image to Dockerhub.


Getting Started

The demo clients are easy to run, you only need make and docker installed to get things rolling.

$ make crossbar # Starts crossbar in a docker container $ make python # Starts a python based WAMP components that provides calls for the Java demo client 

and finally

$ make java # Starts the java (Netty) based demo client that performs WAMP actions 

Show me some code

The code in demo-gallery contains some examples on how to use the autobahn library, it also contains convenience methods to use. Below is a basic set of code examples showing all 4 WAMP actions.

Subscribe to a topic

public void demonstrateSubscribe(Session session, SessionDetails details) {

  // Subscribe to topic to receive its events.
  CompletableFuture<Subscription> subFuture = session.subscribe("com.myapp.hello",

 this::onEvent);

  subFuture.whenComplete((subscription, throwable) -> {

if (throwable != null) {

 // We have successfully subscribed.

 System.out.println("Subscribed to topic " + subscription.topic);

}
 else {

 // Something went bad.

 throwable.printStackTrace();

}

  
}
);
 
}
  private void onEvent(List<Object> args, Map<String, Object> kwargs, EventDetails details) {

  System.out.println(String.format("Got event: %s", args.get(0)));
 
}

Since we are only accessing args in onEvent(), we could simplify it like:

private void onEvent(List<Object> args) {

  System.out.println(String.format("Got event: %s", args.get(0)));
 
}

Publish to a topic

public void demonstratePublish(Session session, SessionDetails details) {

  // Publish to a topic that takes a single arguments
  List<Object> args = Arrays.asList("Hello World!", 900, "UNIQUE");

  CompletableFuture<Publication> pubFuture = session.publish("com.myapp.hello", args);

  pubFuture.thenAccept(publication -> System.out.println("Published successfully"));

  // Shows we can separate out exception handling
  pubFuture.exceptionally(throwable -> {

throwable.printStackTrace();

return null;
  
}
);
 
}

A simpler call would look like:

public void demonstratePublish(Session session, SessionDetails details) {

  CompletableFuture<Publication> pubFuture = session.publish("com.myapp.hello", "Hi!");

  ... 
}

Register a procedure

public void demonstrateRegister(Session session, SessionDetails details) {

  // Register a procedure.
  CompletableFuture<Registration> regFuture = session.register("com.myapp.add2", this::add2);

  regFuture.thenAccept(registration ->

 System.out.println("Successfully registered procedure: " + registration.procedure));
 
}
  private CompletableFuture<InvocationResult> add2(

List<Object> args, Map<String, Object> kwargs, InvocationDetails details) {

  int res = (int) args.get(0) + (int) args.get(1);

  List<Object> arr = new ArrayList<>();

  arr.add(res);

  return CompletableFuture.completedFuture(new InvocationResult(arr));
 
}

A very precise add2 may look like:

private List<Object> add2(List<Integer> args, InvocationDetails details) {

  int res = args.get(0) + args.get(1);

  return Arrays.asList(res, details.session.getID(), "Java");
 
}

Call a procedure

public void demonstrateCall(Session session, SessionDetails details) {

  // Call a remote procedure.
  CompletableFuture<CallResult> callFuture = session.call("com.myapp.add2", 10, 20);

  callFuture.thenAccept(callResult ->

 System.out.println(String.format("Call result: %s", callResult.results.get(0))));
 
}

Connecting the dots

public void main() {

  // Create a session object
  Session session = new Session();

  // Add all onJoin listeners
  session.addOnJoinListener(this::demonstrateSubscribe);

  session.addOnJoinListener(this::demonstratePublish);

  session.addOnJoinListener(this::demonstrateCall);

  session.addOnJoinListener(this::demonstrateRegister);

// finally, provide everything to a Client and connect
  Client client = new Client(session, url, realm);

  CompletableFuture<ExitInfo> exitInfoCompletableFuture = client.connect();
 
}

WebSocket on Android

TBD


Get in touch

Get in touch on IRC #autobahn on chat.freenode.net or join the mailing list.


Version 1

Version 1 of this library is still in the repo here, but is no longer maintained.

Version 1 only supported non-secure WebSocket on Android and only supported WAMP v1.

Both of these issues are fixed in the (current) version of Autobahn|Java.


Resources

Featured helps you to split activity or fragment code into truly decoupled, testable and maintainable features.

A library to work with gregorian lunar calendar.

An ImageView which can auto zoom.

AvatarImageView is a library to display circle with text or circle with image.

IndicatorView allows to indicate current index of page for ViewPager.

This library allows to create dialogs in a material designed style.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes