File tree 2 files changed +56
-0
lines changed
examples/rpc_progressive_call_results
2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
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
+ const int fileSize = 100 ; // Simulate a file size of 100 units
13
+
14
+ for (int i = 0 ; i <= fileSize; i += 10 ) {
15
+ final int progress = i * 100 ~ / fileSize; // Calculate progress percentage
16
+ inv.sendProgress ([progress], null ); // Send progress
17
+ sleep (const Duration (milliseconds: 500 ));
18
+ }
19
+
20
+ return Result (args: ["Download complete!" ]);
21
+ }
22
+
23
+ // Register procedure "io.xconn.progress.download"
24
+ var registration = await session.register (procedureDownload, downloadHandler);
25
+ print ("Registered procedure $procedureDownload successfully" );
26
+
27
+ // Define a signal handler to catch the interrupt signal (Ctrl+C)
28
+ ProcessSignal .sigint.watch ().listen ((signal) async {
29
+ await session.unregister (registration);
30
+ await session.close ();
31
+ });
32
+ }
Original file line number Diff line number Diff line change
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.callProgress (
13
+ procedureDownload,
14
+ (Result result) {
15
+ var progress = result.args[0 ]; // Current progress
16
+ print ("Download progress: $progress %" );
17
+ },
18
+ );
19
+
20
+ print (result.args[0 ]);
21
+
22
+ await session.close ();
23
+ exit (0 );
24
+ }
You can’t perform that action at this time.
0 commit comments