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.



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

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



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

def close
  @driver.close
end

#futures_userinfoObject



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

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



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

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



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

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

#on_openObject

When WebSocket is opened, register callbacks



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

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

#pingpongObject



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

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

#price_api(data) ⇒ Object



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

def price_api(data)
  @driver.text data
end

#userinfoObject



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

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