Skip to content

Commit

Permalink
Add example for progressive call results
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed Oct 16, 2024
1 parent b83ba9c commit f373c90
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/rpc_progressive_call_results/callee.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "dart:io";
import "package:xconn/xconn.dart";

const procedureDownload = "io.xconn.progress.download";

Future<void> 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();
});
}
25 changes: 25 additions & 0 deletions examples/rpc_progressive_call_results/caller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "dart:io";

import "package:xconn/xconn.dart";

const procedureDownload = "io.xconn.progress.download";

Future<void> 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);
}

0 comments on commit f373c90

Please sign in to comment.