Skip to content

Commit 655270f

Browse files
committed
Add some other streaming examples.
1 parent e7cd220 commit 655270f

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2024, by Samuel Williams.
6+
7+
require 'async'
8+
require 'async/http/client'
9+
require 'async/http/server'
10+
require 'async/http/endpoint'
11+
12+
require 'protocol/http/body/streamable'
13+
require 'protocol/http/body/writable'
14+
require 'protocol/http/body/stream'
15+
16+
endpoint = Async::HTTP::Endpoint.parse('http://localhost:3000')
17+
18+
Async do
19+
server = Async::HTTP::Server.for(endpoint) do |request|
20+
output = Protocol::HTTP::Body::Streamable.response(request) do |stream|
21+
$stderr.puts "Server writing chunks..."
22+
stream.write("Hello, ")
23+
stream.write("World!")
24+
25+
$stderr.puts "Server reading chunks..."
26+
while chunk = stream.readpartial(1024)
27+
puts chunk
28+
end
29+
rescue EOFError
30+
$stderr.puts "Server EOF."
31+
# Ignore EOF errors.
32+
ensure
33+
$stderr.puts "Server closing stream."
34+
stream.close
35+
end
36+
37+
Protocol::HTTP::Response[200, {}, output]
38+
end
39+
40+
server_task = Async{server.run}
41+
42+
client = Async::HTTP::Client.new(endpoint)
43+
44+
streamable = Protocol::HTTP::Body::Streamable.request do |stream|
45+
# Simple echo client:
46+
while chunk = stream.readpartial(1024)
47+
$stderr.puts "Client chunk: #{chunk.inspect}"
48+
stream.write(chunk)
49+
$stderr.puts "Client waiting for next chunk..."
50+
end
51+
rescue EOFError
52+
$stderr.puts "Client EOF."
53+
# Ignore EOF errors.
54+
ensure
55+
$stderr.puts "Client closing stream."
56+
stream.close
57+
end
58+
59+
$stderr.puts "Client sending request..."
60+
response = client.get("/", body: streamable)
61+
$stderr.puts "Client received response and streaming it..."
62+
streamable.stream(response.body)
63+
$stderr.puts "Client done streaming response."
64+
ensure
65+
server_task.stop
66+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2024, by Samuel Williams.
6+
7+
require 'async'
8+
require 'async/http/client'
9+
require 'async/http/server'
10+
require 'async/http/endpoint'
11+
12+
require 'protocol/http/body/stream'
13+
require 'protocol/http/body/writable'
14+
15+
def make_server(endpoint)
16+
Async::HTTP::Server.for(endpoint) do |request|
17+
output = Protocol::HTTP::Body::Writable.new
18+
stream = Protocol::HTTP::Body::Stream.new(request.body, output)
19+
20+
Async do
21+
stream.write("Hello, ")
22+
stream.write("World!")
23+
24+
stream.close_write
25+
26+
# Simple echo server:
27+
$stderr.puts "Server reading chunks..."
28+
while chunk = stream.readpartial(1024)
29+
puts chunk
30+
end
31+
rescue EOFError
32+
# Ignore EOF errors.
33+
ensure
34+
stream.close
35+
end
36+
37+
Protocol::HTTP::Response[200, {}, output]
38+
end
39+
end
40+
41+
Async do |task|
42+
endpoint = Async::HTTP::Endpoint.parse('http://localhost:3000')
43+
44+
server_task = task.async{make_server(endpoint).run}
45+
46+
client = Async::HTTP::Client.new(endpoint)
47+
48+
input = Protocol::HTTP::Body::Writable.new
49+
response = client.get("/", body: input)
50+
51+
begin
52+
stream = Protocol::HTTP::Body::Stream.new(response.body, input)
53+
54+
$stderr.puts "Client echoing chunks..."
55+
while chunk = stream.readpartial(1024)
56+
stream.write(chunk)
57+
end
58+
rescue EOFError
59+
# Ignore EOF errors.
60+
ensure
61+
stream.close
62+
end
63+
ensure
64+
server_task.stop
65+
end

0 commit comments

Comments
 (0)