|
| 1 | +package io.crossbar.autobahn.demogallery; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | + |
| 8 | +import io.crossbar.autobahn.wamp.Client; |
| 9 | +import io.crossbar.autobahn.wamp.Session; |
| 10 | +import io.crossbar.autobahn.wamp.types.CallOptions; |
| 11 | +import io.crossbar.autobahn.wamp.types.CallResult; |
| 12 | +import io.crossbar.autobahn.wamp.types.ExitInfo; |
| 13 | +import io.crossbar.autobahn.wamp.types.InvocationDetails; |
| 14 | +import io.crossbar.autobahn.wamp.types.InvocationResult; |
| 15 | +import io.crossbar.autobahn.wamp.types.Registration; |
| 16 | + |
| 17 | +public class ProgressiveCallResultsExample { |
| 18 | + |
| 19 | + public static CompletableFuture<ExitInfo> registerProgressive(String wsAddress, String realm) { |
| 20 | + Session wampSession = new Session(); |
| 21 | + wampSession.addOnJoinListener((session, details) -> { |
| 22 | + CompletableFuture<Registration> regFuture = session.register( |
| 23 | + "io.crossbar.longop", |
| 24 | + (List<Object> args, Map<String, Object> kwargs, InvocationDetails invocationDetails) -> { |
| 25 | + for (int i = 0; i < 5; i++) { |
| 26 | + List<Object> argsList = new ArrayList<>(); |
| 27 | + argsList.add(i); |
| 28 | + invocationDetails.progress.sendProgress(argsList, null); |
| 29 | + } |
| 30 | + List<Object> resultArgs = new ArrayList<>(); |
| 31 | + resultArgs.add(7); |
| 32 | + return CompletableFuture.completedFuture(new InvocationResult(resultArgs)); |
| 33 | + }); |
| 34 | + |
| 35 | + regFuture.whenComplete((registration, throwable) -> { |
| 36 | + System.out.println(String.format( |
| 37 | + "Registered procedure %s", registration.procedure)); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + Client wampClient = new Client(wampSession, wsAddress, realm); |
| 42 | + return wampClient.connect(); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + public static CompletableFuture<ExitInfo> callProgressive(String wsAddress, String realm) { |
| 47 | + Session wampSession = new Session(); |
| 48 | + wampSession.addOnJoinListener((session, details) -> { |
| 49 | + CompletableFuture<CallResult> callFuture = session.call( |
| 50 | + "io.crossbar.longop", |
| 51 | + new CallOptions(result -> System.out.println("Receive Progress: " + result.results))); |
| 52 | + |
| 53 | + callFuture.whenComplete((callResult, throwable) -> { |
| 54 | + System.out.println(String.format("Call result: %s", callResult.results)); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + Client wampClient = new Client(wampSession, wsAddress, realm); |
| 59 | + return wampClient.connect(); |
| 60 | + } |
| 61 | +} |
0 commit comments