socketry / async-http

MIT License
298 stars 45 forks source link

Example in README does not work #3

Closed yfractal closed 6 years ago

yfractal commented 6 years ago

I run code as below:

endpoint        = Async::IO::Endpoint.tcp('127.0.0.1', 9294, reuse_port: true)
client_endpoint = Async::HTTP::URLEndpoint.parse("127.0.0.1/9294", reuse_port: true) 
server          = Server.new(endpoint)
client          = Async::HTTP::Client.new(client_endpoint)

class Server < Async::HTTP::Server
  def handle_request(request, peer, address)
    [200, {}, ["Hello World"]]
  end
end

Async::Reactor.run do |task|
  server_task = task.async do
    server.run
  end

  response = client.get("/", {})
  puts response.body

  server_task.stop
end

But get error: ERROR -- : Connection refused - connect(2) for [::1]:80:

The Async::IO::VERSION is "1.6.1"

I use Async::HTTP::URLEndpoint.parse to get client endpoint because Async::HTTP::Client needs endpoint has protocol method.

ioquatix commented 6 years ago

Thanks for your report. Okay I will check. I've been pretty busy with this code base the past few days, things are converging on a 1.0 release. It would be good to have some more examples.

ioquatix commented 6 years ago

Okay, here is the working example. I'll check with what's in the readme, but the original code has some issues.

#!/usr/bin/env ruby

require 'async/reactor'
require 'async/http/client'
require 'async/http/server'
require 'async/http/url_endpoint'

class Server < Async::HTTP::Server
  def handle_request(request, peer, address)
    [200, {}, ["Hello World"]]
  end
end

endpoint = Async::IO::Endpoint.tcp('127.0.0.1', 9294, reuse_port: true)
client_endpoint = Async::HTTP::URLEndpoint.parse("http://127.0.0.1:9294", reuse_port: true) 
server = Server.new(endpoint)
client = Async::HTTP::Client.new(client_endpoint)

Async::Reactor.run do |task|
  server_task = task.async do
    server.run
  end

  response = client.get("/", {})
  puts response.read

  server_task.stop
end
ioquatix commented 6 years ago

Okay I've updated the README example and it's working well.

yfractal commented 6 years ago

Sorry, just notice your comments ...

Yeah, it works! Thank you. The async-* gems look great 👍