First of all, I'm a complete newbie in Java. What I want to achieve is wait for async results before I return them.
MainActivity.java:
...
private SendTransport.Listener sendTransportListener = new SendTransport.Listener() {
@Override
public String onProduce(JSONObject data) {
String result = receiveResult(data);
return result;
}
};
private String receiveResult(JSONObject data) throws InterruptedException {
String result = "";
final CountDownLatch latch = new CountDownLatch(1);
socket.emit("receive-result", data, new Ack() {
@Override
public void call(Object... args) {
result = args[1];
latch.countDown();
}
});
latch.await();
return result;
}
...
In the code above, the socket.emit
function never executes. What's wrong with my code? Or better to use some other approach here? If yes, then what?