Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to negotiate connection: 400 #27

Closed
googya opened this issue Oct 22, 2020 · 2 comments
Closed

Failed to negotiate connection: 400 #27

googya opened this issue Oct 22, 2020 · 2 comments

Comments

@googya
Copy link

googya commented Oct 22, 2020

Did i use a wrong way? i did not figure it out how to connect a endpoint

Async do |task|
	endpoint = Async::HTTP::Endpoint.parse(URL)
	
	Async::WebSocket::Client.connect(endpoint) do |connection|
		connection.write ["Hello World!", "ping"]
		
		while message = connection.read
			p message
		end
	end
end
 0.91s    error: Async::Task [oid=0x2a8] [pid=70589] [2020-10-22 22:49:22 +0900]
               |   Async::WebSocket::ProtocolError: Failed to negotiate connection: 400
               |   → /Users/kv/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/async-websocket-0.16.0/lib/async/websocket/client.rb:99 in `connect'
               |     /Users/kv/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/async-websocket-0.16.0/lib/async/websocket/client.rb:53 in `block in connect'
               |     /Users/kv/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/async-websocket-0.16.0/lib/async/websocket/client.rb:44 in `open'
               |     /Users/kv/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/async-websocket-0.16.0/lib/async/websocket/client.rb:52 in `connect'
               |     t.rb:14 in `block in <main>'
               |     /Users/kv/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/async-1.26.2/lib/async/task.rb:258 in `block in make_fiber'
@googya
Copy link
Author

googya commented Oct 22, 2020

using iodine seems very easy :

require 'iodine'

class PriceClient
  def on_message(connection, message)
    puts "Received: #{message}"
  end

  def on_close(connection)
    puts "* Client closed."
    Iodine.stop
  end
end

Iodine.threads = 1
Iodine.connect url: "wss://stream.binance.com:9443/ws/btcusdt@bookTicker", handler: PriceClient.new, ping: 40
Iodine.start

@ioquatix
Copy link
Member

It looks like they support HTTP/2 but they don't support WebSockets over HTTP/2.

You need to force the client to only use HTTP/1:

#!/usr/bin/env ruby

require 'async'
require 'async/http'
require 'async/websocket'

URL = "wss://stream.binance.com:9443/ws/btcusdt@bookTicker"

Async do |task|
	endpoint = Async::HTTP::Endpoint.parse(URL, alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
	
	Async::WebSocket::Client.connect(endpoint) do |connection|
		while message = connection.read
			p message
		end
	end
end

The reason why Iodine works is because it only supports HTTP/1 AFAIK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants