Class: Bitstamp::Websocket
Constant Summary
collapse
- BASE_URI =
'wss://ws.pusherapp.com/app/de504dc5763aeef9ff52'
- CLIENT_ID =
'bitstamp-client'
- PROTOCOL =
7
- PARAMS =
"?client=#{CLIENT_ID}&version=#{Bitstamp::VERSION}&protocol=#{PROTOCOL}"
Instance Method Summary
collapse
#live_trades
#live_full_order_book, #live_order_book, #live_orders_changed, #live_orders_created, #live_orders_deleted
Methods included from Handler
#handle_body
Constructor Details
#initialize(logger) ⇒ Websocket
Returns a new instance of Websocket.
19
20
21
|
# File 'lib/bitstamp/websocket.rb', line 19
def initialize(logger)
@logger = logger
end
|
Instance Method Details
#listen(channel, event, block) ⇒ Object
23
24
25
26
27
|
# File 'lib/bitstamp/websocket.rb', line 23
def listen(channel, event, block)
EM.run do
setup_socket(channel, event, block)
end
end
|
#setup_socket(channel, event, block) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/bitstamp/websocket.rb', line 29
def setup_socket(channel, event, block)
websocket = Faye::WebSocket::Client.new(BASE_URI + PARAMS)
websocket.on(:open) do |message|
subscribe(websocket, channel)
@logger.debug("Opened connection and subscribed to '#{channel}'")
end
websocket.on(:message) do |message|
parsed_message = handle_body(message.data)
case parsed_message.fetch('event')
when event
data = handle_body(parsed_message.fetch('data'))
block.call(data)
when 'pusher:connection_established'
@logger.debug('Connection established')
when 'pusher_internal:subscription_succeeded'
@logger.debug("Subscription to channel '#{channel}' succeeded")
else
@logger.debug("Received unhandled message: #{message.data.to_s}")
end
end
websocket.on(:close) do |message|
@logger.debug("Closed websocket connection")
end
end
|
#subscribe(websocket, channel) ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'lib/bitstamp/websocket.rb', line 58
def subscribe(websocket, channel)
subscribe_message = {
event: 'pusher:subscribe',
data: { channel: channel }
}
websocket.send(subscribe_message.to_json)
end
|