Following section provides samples about connecting to Josceleton with different approaches, which are:
Josceleton.openConnection()
The following code snippet uses the facade class Josceleton to open a connection (on the default port 7110) and add a listener for arbitrary messages and any user:
Connection connection = Josceleton.openConnection(); connection.addListener(new ConnectionListener() { public void onUserMessage(UserMessage userMessage) { String output = "[user " + userMessage.getUser().getOsceletonId() + "] "; output += userMessage.getUserState().name(); System.out.println(output); // prints "[user 1] WAITING" (or PROCESSING, DEAD) } public void onJointMessage(JointMessage jointMessage) { // this will be invoked quiet often ;) String output = "[user " + jointMessage.getUser().getOsceletonId() + "] moved"; if(jointMessage.getJointPart() == Body.HAND().LEFT()) { output += "left hand: "; } output += jointMessage.getCoordinate().x() + "/"; output += jointMessage.getCoordinate().y() + "/"; output += jointMessage.getCoordinate().z(); System.out.println(output); // prints "[user 1] moved left hand: 0.342/0.981/1.524" } }); System.out.println("Running ..."); // as the connection is asynchronously we have to close it as well some time later // connection.close(); |
Josceleton.newFacade()
Create a JosceletonFacade instance and pass it (or inject it) into your classes as needed. This approach is slightly better, as the static access is reduced to one single location.
The following snippet shows the evolution process of how to access such an instance in four steps. The final step is actually equivalent to the first one; this is just what is going on behind the scenes.
// #1 - this is the most common way you will use (recommended approach) // =============================================================================== JosceletonFacade facade = Josceleton.newFacade(); ... = facade.openConnection(); // #2 - internally instantiates a facade implementation // =============================================================================== JosceletonFacade facade = new JosceletonFacadeImpl(Josceleton.newGuiceInjector()); ... = facade.openConnection(); // #3 - then invokes guice directly to create an injector // =============================================================================== Injector injector = Guice.createInjector(new JosceletonGuiceModule()); JosceletonFacade facade = new JosceletonFacadeImpl(injector); ... = facade.openConnection(); // #4 - tweak: outline module creation (this represents what is actually done internally) // =============================================================================== Module module = new JosceletonGuiceModule(); Injector injector = Guice.createInjector(module); JosceletonFacade facade = new JosceletonFacadeImpl(injector); ... = facade.openConnection(); |