-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for progressive call results
- Loading branch information
1 parent
b83ba9c
commit f373c90
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |