Created
October 13, 2025 01:26
-
-
Save ailabs-software/fb72cf67ffa5d4c9ec0b004230ee23f3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const url = 'http://localhost:8080'; | |
| fetch(url) | |
| .then(response => { | |
| const reader = response.body.getReader(); | |
| const decoder = new TextDecoder(); | |
| function readChunk() { | |
| reader.read().then(({done, value}) => { | |
| if (done) { | |
| console.log('Stream closed'); | |
| return; | |
| } | |
| // Print each chunk as it arrives | |
| const text = decoder.decode(value); | |
| console.log(text); | |
| readChunk(); | |
| }); | |
| } | |
| readChunk(); | |
| }); |
This file contains hidden or 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
| import 'dart:async'; | |
| import 'dart:io'; | |
| Future<void> main() async { | |
| final server = await HttpServer.bind(InternetAddress.anyIPv4, 8080); | |
| print('Listening on localhost:${server.port}'); | |
| await for (HttpRequest request in server) { | |
| // Set the response to chunked mode | |
| request.response | |
| ..headers.contentType = ContentType.text | |
| ..headers.set('Cache-Control', 'no-cache') | |
| ..headers.set('Transfer-Encoding', 'chunked'); | |
| // Send a chunk of text every second | |
| Timer.periodic(Duration(seconds: 1), (timer) { | |
| final message = 'Chunk at ${DateTime.now()}\n'; | |
| request.response.write(message); | |
| // Flush to send data immediately | |
| request.response.flush(); | |
| // Stop after 10 chunks for this example | |
| if (timer.tick >= 10) { | |
| request.response.close(); | |
| timer.cancel(); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment