HoneyryderChuck 84db0072fb new :stream_bidi plugin
this plugin is an HTTP/2 only plugin which enables bidirectional streaming

the client can continue writing request streams as response streams arrive midway

Closes https://github.com/HoneyryderChuck/httpx/discussions/71
2025-04-04 00:21:12 +01:00

28 lines
648 B
Ruby

# frozen_string_literal: true
require_relative "test"
class Bidi < TestHTTP2Server
private
def handle_stream(_conn, stream)
stream.on(:data) do |d|
next if d.empty?
# puts "SERVER: payload chunk: <<#{d}>>"
data = JSON.parse(d)
stream.data(JSON.dump({ processed: data }) << "\n", end_stream: false)
end
stream.on(:half_close) do
stream.data("", end_stream: true)
end
stream.headers({
":status" => "200",
"date" => Time.now.httpdate,
"content-type" => "application/x-ndjson",
}, end_stream: false)
end
end