From f373c90ee664c09f292fd599a9ed8593576450e1 Mon Sep 17 00:00:00 2001 From: Muzzammil Shahid Date: Wed, 16 Oct 2024 16:17:30 +0500 Subject: [PATCH] Add example for progressive call results --- .../rpc_progressive_call_results/callee.dart | 34 +++++++++++++++++++ .../rpc_progressive_call_results/caller.dart | 25 ++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 examples/rpc_progressive_call_results/callee.dart create mode 100644 examples/rpc_progressive_call_results/caller.dart diff --git a/examples/rpc_progressive_call_results/callee.dart b/examples/rpc_progressive_call_results/callee.dart new file mode 100644 index 0000000..00e1396 --- /dev/null +++ b/examples/rpc_progressive_call_results/callee.dart @@ -0,0 +1,34 @@ +import "dart:io"; +import "package:xconn/xconn.dart"; + +const procedureDownload = "io.xconn.progress.download"; + +Future main() async { + var client = Client(); + var session = await client.connect("ws://localhost:8080/ws", "realm1"); + + // Define function to handle received Invocation for "io.xconn.progress.download" + Result downloadHandler(Invocation inv) { + var totalSize = 1000; // Total file size in "bytes" + var chunkSize = 100; // Each chunk is 100 bytes + var progress = 0; + + while (progress < totalSize) { + progress += chunkSize; + inv.sendProgress([progress, totalSize], null); // Send progress + sleep(const Duration(milliseconds: 500)); // Simulate time to download each chunk + } + + return Result(args: ["Download complete"]); + } + + // Register procedure "io.xconn.progress.download" + var registration = await session.register(procedureDownload, downloadHandler); + print("Registered procedure $procedureDownload successfully"); + + // Define a signal handler to catch the interrupt signal (Ctrl+C) + ProcessSignal.sigint.watch().listen((signal) async { + await session.unregister(registration); + await session.close(); + }); +} diff --git a/examples/rpc_progressive_call_results/caller.dart b/examples/rpc_progressive_call_results/caller.dart new file mode 100644 index 0000000..b43a94b --- /dev/null +++ b/examples/rpc_progressive_call_results/caller.dart @@ -0,0 +1,25 @@ +import "dart:io"; + +import "package:xconn/xconn.dart"; + +const procedureDownload = "io.xconn.progress.download"; + +Future main() async { + var client = Client(); + var session = await client.connect("ws://localhost:8080/ws", "realm1"); + + // Call procedure "io.xconn.progress.download" + var result = await session.call( + procedureDownload, + progressHandler: (Result result) { + var progress = result.args[0]; // Current progress + var totalSize = result.args[1]; // Total file size + print("Download progress: $progress / $totalSize bytes"); + }, + ); + + print(result.args[0]); + + await session.close(); + exit(0); +}