Skip to content

Commit

Permalink
Add some other streaming examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 10, 2024
1 parent e7cd220 commit 655270f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/streaming/bidirectional2.rb
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions examples/streaming/unidirectional2.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 655270f

Please sign in to comment.