Closed googya closed 3 years ago
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
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.
Did i use a wrong way? i did not figure it out how to connect a endpoint