Class: Binance::WebSocket

Inherits:
Faye::WebSocket::Client
  • Object
show all
Defined in:
lib/binance/sdk/websocket.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(on_open: nil, on_close: nil) ⇒ WebSocket

Returns a new instance of WebSocket.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/binance/sdk/websocket.rb', line 7

def initialize(on_open: nil, on_close: nil)
  websocket_api_url = if SDK::Configuration.futures?
          'wss://fstream.binance.com/stream'
        else
          'wss://stream.binance.com:9443/stream'
        end
  super websocket_api_url, nil, ping: 180

  @request_id_inc = 0
  @user_stream_handlers = {}

  on :open do |event|
    on_open&.call(event)
  end

  on :message do |event|
    process_data(event.data)
  end

  on :close do |event|
    on_close&.call(event)
  end
end

Instance Method Details

#candlesticks!(symbols, interval, &on_receive) ⇒ Object

stream name: <symbol>@kline_<interval>

{

"e": "kline",     // Event type
"E": 123456789,   // Event time
"s": "BTCUSDT",    // Symbol
"k": {
  "t": 123400000, // Kline start time
  "T": 123460000, // Kline close time
  "s": "BTCUSDT",  // Symbol
  "i": "1m",      // Interval
  "f": 100,       // First trade ID
  "L": 200,       // Last trade ID
  "o": "0.0010",  // Open price
  "c": "0.0020",  // Close price
  "h": "0.0025",  // High price
  "l": "0.0015",  // Low price
  "v": "1000",    // Base asset volume
  "n": 100,       // Number of trades
  "x": false,     // Is this kline closed?
  "q": "1.0000",  // Quote asset volume
  "V": "500",     // Taker buy base asset volume
  "Q": "0.500",   // Taker buy quote asset volume
  "B": "123456"   // Ignore
}

}



58
59
60
61
62
# File 'lib/binance/sdk/websocket.rb', line 58

def candlesticks!(symbols, interval, &on_receive)
  symbols_fmt = symbols.is_a?(String) ? [symbols] : symbols
  @candlesticks_handler = on_receive
  subscribe(symbols_fmt.map { |s| "#{s.downcase}@kline_#{interval}" })
end

#user_data_stream!(listen_key, &on_receive) ⇒ Object



64
65
66
67
# File 'lib/binance/sdk/websocket.rb', line 64

def user_data_stream!(listen_key, &on_receive)
  @user_stream_handlers[listen_key] = on_receive
  subscribe([listen_key])
end