Class: BinanceAPI::Stream
- Inherits:
-
Object
- Object
- BinanceAPI::Stream
- Defined in:
- lib/binance_api/stream.rb
Constant Summary collapse
- BASE_ENDPOINT =
'wss://stream.binance.com:9443/'.freeze
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(endpoints, on_open: nil, on_close: nil, on_error: nil, on_message: nil) ⇒ Stream
constructor
A new instance of Stream.
- #start ⇒ Object
Constructor Details
#initialize(endpoints, on_open: nil, on_close: nil, on_error: nil, on_message: nil) ⇒ Stream
Returns a new instance of Stream.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/binance_api/stream.rb', line 6 def initialize(endpoints, on_open: nil, on_close: nil, on_error: nil, on_message: nil) raise 'on_open can accept a block only' if !on_open.nil? && !on_open.is_a?(Proc) raise 'on_close can accept a block only' if !on_close.nil? && !on_close.is_a?(Proc) raise 'on_error can accept a block only' if !on_error.nil? && !on_error.is_a?(Proc) raise 'on_data can accept a block only' if !.nil? && !.is_a?(Proc) @endpoints = Array(endpoints).reject(&:nil?).reject(&:empty?) raise 'endpoints cannot be empty' if @endpoints.empty? @on_open = on_open @on_close = on_close @on_error = on_error @on_message = @stream_type = @endpoints.length > 1 ? :combined : :single end |
Instance Method Details
#close ⇒ Object
31 32 33 |
# File 'lib/binance_api/stream.rb', line 31 def close ws.close end |
#start ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/binance_api/stream.rb', line 22 def start @ws = WebSocket::Client::Simple.connect "#{BASE_ENDPOINT}#{stream_url}" ws.on :message, & ws.on :open, &on_open ws.on :close, &on_close ws.on :error, &on_error ws end |