Skip to content

Commit e6bd810

Browse files
Add example for progressive call results
1 parent 1c2a001 commit e6bd810

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import "dart:io";
2+
import "package:xconn/xconn.dart";
3+
4+
const procedureDownload = "io.xconn.progress.download";
5+
6+
Future<void> main() async {
7+
var client = Client();
8+
var session = await client.connect("ws://localhost:8080/ws", "realm1");
9+
10+
// Define function to handle received Invocation for "io.xconn.progress.download"
11+
Result downloadHandler(Invocation inv) {
12+
var totalSize = 1000; // Total file size in "bytes"
13+
var chunkSize = 100; // Each chunk is 100 bytes
14+
var progress = 0;
15+
16+
while (progress < totalSize) {
17+
progress += chunkSize;
18+
inv.sendProgress([progress, totalSize], null); // Send progress
19+
sleep(const Duration(milliseconds: 500)); // Simulate time to download each chunk
20+
}
21+
22+
return Result(args: ["Download complete"]);
23+
}
24+
25+
// Register procedure "io.xconn.progress.download"
26+
var registration = await session.register(procedureDownload, downloadHandler);
27+
print("Registered procedure $procedureDownload successfully");
28+
29+
// Define a signal handler to catch the interrupt signal (Ctrl+C)
30+
ProcessSignal.sigint.watch().listen((signal) async {
31+
await session.unregister(registration);
32+
await session.close();
33+
});
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import "dart:io";
2+
3+
import "package:xconn/xconn.dart";
4+
5+
const procedureDownload = "io.xconn.progress.download";
6+
7+
Future<void> main() async {
8+
var client = Client();
9+
var session = await client.connect("ws://localhost:8080/ws", "realm1");
10+
11+
// Call procedure "io.xconn.progress.download"
12+
var result = await session.call(
13+
procedureDownload,
14+
progressHandler: (Result result) {
15+
var progress = result.args[0]; // Current progress
16+
var totalSize = result.args[1]; // Total file size
17+
print("Download progress: $progress / $totalSize bytes");
18+
},
19+
);
20+
21+
print(result.args[0]);
22+
23+
await session.close();
24+
exit(0);
25+
}

0 commit comments

Comments
 (0)