}, new Action1<Throwable>() {
@Override
public void call(Throwable e) {
System.out.println("failed to subscribe 'onhello': " + e);
}
}, new Action0() {
@Override
public void call() {
System.out.println("'onhello' subscription ended");
}
});
// REGISTER a procedure for remote calling
addProcSubscription = client.registerProcedure("com.example.add2")
.observeOn(rxScheduler)
.subscribe(new Action1<Request>() {
@Override
public void call(Request request) {
if (request.arguments() == null || request.arguments().size() != 2
|| !request.arguments().get(0).canConvertToLong()
|| !request.arguments().get(1).canConvertToLong())
{
try {
request.replyError(new ApplicationError(ApplicationError.INVALID_PARAMETER));
} catch (ApplicationError e) { }
}
else {
long a = request.arguments().get(0).asLong();
long b = request.arguments().get(1).asLong();
request.reply(a + b);
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable e) {
System.out.println("failed to register procedure: " + e);
}
}, new Action0() {
@Override
public void call() {
System.out.println("procedure subscription ended");
}
});
// PUBLISH and CALL every second .. forever
counter = 0;
counterPublication = rxScheduler.createWorker().schedulePeriodically(new Action0() {
@Override
public void call() {
// PUBLISH an event
final int published = counter;
client.publish("com.example.oncounter", published)
.observeOn(rxScheduler)
.subscribe(new Action1<Long>() {
@Override
public void call(Long t1) {
System.out.println("published to 'oncounter' with counter " + published);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable e) {
System.out.println("Error during publishing to 'oncounter': " + e);
}
});
// CALL a remote procedure
client.call("com.example.mul2", Long.class, counter, 3)
.observeOn(rxScheduler)
.subscribe(new Action1<Long>() {
@Override
public void call(Long result) {
System.out.println("mul2() called with result: " + result);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable e) {
boolean isProcMissingError = false;
if (e instanceof ApplicationError) {
if (((ApplicationError) e).uri().equals("wamp.error.no_such_procedure"))
isProcMissingError = true;
}
if (!isProcMissingError) {
System.out.println("call of mul2() failed: " + e);
}
}
});
counter++;
}
}, TIMER_INTERVAL, TIMER_INTERVAL, TimeUnit.MILLISECONDS);
}
else if (t1 == WampClient.Status.Disconnected) {
closeSubscriptions();
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable t) {
System.out.println("Session ended with error " + t);
}
}, new Action0() {
@Override
public void call() {
System.out.println("Session ended normally");
}
});