From 655270f3fb6c6cc66a05b88245b36d48a12f2a0f Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 11 Sep 2024 10:36:15 +1200 Subject: [PATCH] Add some other streaming examples. --- examples/streaming/bidirectional2.rb | 66 +++++++++++++++++++++++++++ examples/streaming/unidirectional2.rb | 65 ++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100755 examples/streaming/bidirectional2.rb create mode 100755 examples/streaming/unidirectional2.rb diff --git a/examples/streaming/bidirectional2.rb b/examples/streaming/bidirectional2.rb new file mode 100755 index 0000000..dd9e675 --- /dev/null +++ b/examples/streaming/bidirectional2.rb @@ -0,0 +1,66 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2024, by Samuel Williams. + +require 'async' +require 'async/http/client' +require 'async/http/server' +require 'async/http/endpoint' + +require 'protocol/http/body/streamable' +require 'protocol/http/body/writable' +require 'protocol/http/body/stream' + +endpoint = Async::HTTP::Endpoint.parse('http://localhost:3000') + +Async do + server = Async::HTTP::Server.for(endpoint) do |request| + output = Protocol::HTTP::Body::Streamable.response(request) do |stream| + $stderr.puts "Server writing chunks..." + stream.write("Hello, ") + stream.write("World!") + + $stderr.puts "Server reading chunks..." + while chunk = stream.readpartial(1024) + puts chunk + end + rescue EOFError + $stderr.puts "Server EOF." + # Ignore EOF errors. + ensure + $stderr.puts "Server closing stream." + stream.close + end + + Protocol::HTTP::Response[200, {}, output] + end + + server_task = Async{server.run} + + client = Async::HTTP::Client.new(endpoint) + + streamable = Protocol::HTTP::Body::Streamable.request do |stream| + # Simple echo client: + while chunk = stream.readpartial(1024) + $stderr.puts "Client chunk: #{chunk.inspect}" + stream.write(chunk) + $stderr.puts "Client waiting for next chunk..." + end + rescue EOFError + $stderr.puts "Client EOF." + # Ignore EOF errors. + ensure + $stderr.puts "Client closing stream." + stream.close + end + + $stderr.puts "Client sending request..." + response = client.get("/", body: streamable) + $stderr.puts "Client received response and streaming it..." + streamable.stream(response.body) + $stderr.puts "Client done streaming response." +ensure + server_task.stop +end diff --git a/examples/streaming/unidirectional2.rb b/examples/streaming/unidirectional2.rb new file mode 100755 index 0000000..a2abd96 --- /dev/null +++ b/examples/streaming/unidirectional2.rb @@ -0,0 +1,65 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2024, by Samuel Williams. + +require 'async' +require 'async/http/client' +require 'async/http/server' +require 'async/http/endpoint' + +require 'protocol/http/body/stream' +require 'protocol/http/body/writable' + +def make_server(endpoint) + Async::HTTP::Server.for(endpoint) do |request| + output = Protocol::HTTP::Body::Writable.new + stream = Protocol::HTTP::Body::Stream.new(request.body, output) + + Async do + stream.write("Hello, ") + stream.write("World!") + + stream.close_write + + # Simple echo server: + $stderr.puts "Server reading chunks..." + while chunk = stream.readpartial(1024) + puts chunk + end + rescue EOFError + # Ignore EOF errors. + ensure + stream.close + end + + Protocol::HTTP::Response[200, {}, output] + end +end + +Async do |task| + endpoint = Async::HTTP::Endpoint.parse('http://localhost:3000') + + server_task = task.async{make_server(endpoint).run} + + client = Async::HTTP::Client.new(endpoint) + + input = Protocol::HTTP::Body::Writable.new + response = client.get("/", body: input) + + begin + stream = Protocol::HTTP::Body::Stream.new(response.body, input) + + $stderr.puts "Client echoing chunks..." + while chunk = stream.readpartial(1024) + stream.write(chunk) + end + rescue EOFError + # Ignore EOF errors. + ensure + stream.close + end +ensure + server_task.stop +end