Class: Okcoin::WS

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/okcoin/ws.rb

Constant Summary collapse

BASE_URI =
'wss://real.okcoin.com:10440/websocket/okcoinapi'

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, secret_key: nil) ⇒ WS

Returns a new instance of WS.



6
7
8
9
10
# File 'lib/okcoin/ws.rb', line 6

def initialize(api_key: nil, secret_key: nil)
  @driver = Celluloid::WebSocket::Client.new(BASE_URI, Celluloid::Actor.current)
  @api_key = api_key
  @secret_key = secret_key
end

Instance Method Details

#closeObject

close WebSocket connection



31
32
33
# File 'lib/okcoin/ws.rb', line 31

def close
  @driver.close
end

#futures_userinfoObject



44
45
46
47
# File 'lib/okcoin/ws.rb', line 44

def futures_userinfo
  post_data = initial_post_data
  emit(event: 'addChannel', channel: 'ok_futureusd_userinfo', post_data: post_data)
end

#on_close(code, reason) ⇒ Object

When WebSocket is closed



24
25
26
27
28
# File 'lib/okcoin/ws.rb', line 24

def on_close(code, reason)
  puts "WebSocket connection closed: #{code.inspect}, #{reason.inspect}"
  @driver.terminate
  terminate
end

#on_message(msg) ⇒ Object

When raw WebSocket message is received



19
20
21
# File 'lib/okcoin/ws.rb', line 19

def on_message(msg)
  puts "Message received: #{msg}"
end

#on_openObject

When WebSocket is opened, register callbacks



14
15
16
# File 'lib/okcoin/ws.rb', line 14

def on_open
 puts "Websocket connection to #{BASE_URI} established succesfully"
end

#pingpongObject



35
36
37
# File 'lib/okcoin/ws.rb', line 35

def pingpong
  every(5){ @driver.text "{'event':'ping'}" }
end

#price_api(data) ⇒ Object



49
50
51
# File 'lib/okcoin/ws.rb', line 49

def price_api(data)
  @driver.text data
end

#userinfoObject



39
40
41
42
# File 'lib/okcoin/ws.rb', line 39

def userinfo
  post_data = initial_post_data
  emit(event: 'addChannel', channel: 'ok_spotusd_userinfo', post_data: post_data)
end