@@ -39,7 +39,8 @@ Future<String> httpGet(String uri) async {
39
39
}
40
40
41
41
// Start a HTTP server on a background thread.
42
- void httpServe (void Function (String ) onRequest) {
42
+ // Returns a function that should be called to stop the server.
43
+ Function httpServe (void Function (String ) onRequest) {
43
44
// Create the NativeCallable.listener.
44
45
void onNativeRequest (Pointer <Utf8 > requestPointer) {
45
46
onRequest (requestPointer.toDartString ());
@@ -50,13 +51,12 @@ void httpServe(void Function(String) onRequest) {
50
51
// Invoke the native function to start the HTTP server. Our example
51
52
// HTTP library will start a server on a background thread, and pass
52
53
// any requests it receives to out callback.
53
- nativeHttpServe (callback.nativeFunction);
54
+ nativeHttpStartServing (callback.nativeFunction);
54
55
55
- // The server will run indefinitely, and the callback needs to stay
56
- // alive for that whole time, so we can't close the callback here.
57
- // But we also don't want the callback to keep the isolate alive
58
- // forever, so we set keepIsolateAlive to false.
59
- callback.keepIsolateAlive = false ;
56
+ return () {
57
+ nativeHttpStopServing ();
58
+ callback.close ();
59
+ };
60
60
}
61
61
62
62
// Load the native functions from a DynamicLibrary.
@@ -73,12 +73,21 @@ typedef HttpGetNativeFunction = Void Function(
73
73
final nativeHttpGet =
74
74
dylib.lookupFunction <HttpGetNativeFunction , HttpGetFunction >('http_get' );
75
75
76
- typedef HttpServeFunction = void Function (
76
+ typedef HttpStartServingFunction = bool Function (
77
77
Pointer <NativeFunction <HttpCallback >>);
78
- typedef HttpServeNativeFunction = Void Function (
78
+ typedef HttpStartServingNativeFunction = Bool Function (
79
79
Pointer <NativeFunction <HttpCallback >>);
80
- final nativeHttpServe = dylib
81
- .lookupFunction <HttpServeNativeFunction , HttpServeFunction >('http_serve' );
80
+ final nativeHttpStartServing = dylib
81
+ .lookupFunction <HttpStartServingNativeFunction , HttpStartServingFunction >(
82
+ 'http_start_serving' ,
83
+ );
84
+
85
+ typedef HttpStopServingFunction = void Function ();
86
+ typedef HttpStopServingNativeFunction = Void Function ();
87
+ final nativeHttpStopServing = dylib
88
+ .lookupFunction <HttpStopServingNativeFunction , HttpStopServingFunction >(
89
+ 'http_stop_serving' ,
90
+ );
82
91
83
92
Future <void > main () async {
84
93
print ('Sending GET request...' );
0 commit comments