I'm trying to communicate with CoinEx (cryptocurrency exchange) API via WebSocket using Ruby 2.6. I have the following code:
require 'faye/websocket'
require 'eventmachine'
url = 'wss://socket.coinex.com/'
# url = 'wss://stream.binance.com:9443/ws/ltcbtc@miniTicker'
EM.run do
ws = Faye::WebSocket::Client.new(url)
ws.on :open do |event|
p [:open]
...
end
ws.on :message do |event|
p [:message]
...
end
ws.on :close do |event|
p [:close, event.code, event.reason]
...
end
end
When I run this code, I always see [:close, 1006, ""] immediately, without [:open]. I hacked deeply into gems (faye/websocket and eventmachine) and added some debug output to have better understanding of what is going on. Now my traceback is as follows:
/home/chernish2/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/faye-websocket-0.10.9/lib/faye/websocket/client.rb:87:in `unbind'
/home/chernish2/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/eventmachine-1.2.7/lib/eventmachine.rb:1483:in `event_callback'
/home/chernish2/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/eventmachine-1.2.7/lib/eventmachine.rb:195:in `run_machine'
/home/chernish2/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/eventmachine-1.2.7/lib/eventmachine.rb:195:in `run'
/home/chernish2/soft/trader2/test/ws_test.rb:36:in `<main>'
emit_error(), message=Errno::ENETUNREACH
[:close, 1006, ""]
Which really doesn't make any sense to me since another URL
url = 'wss://stream.binance.com:9443/ws/ltcbtc@miniTicker'
works just fine, and when I'm using https://github.com/altangent/ccxws library (nodejs) it connects to CoinEx using exactly the same URL as in my code without any troubles which means I don't have problems connecting to CoinEx WS endpoint. So what is wrong with my code? Thank you in advance!
Oh, it is working now, without any changes from my side. Seems like there was some problems on the server side.