-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebsocketClient.rb
97 lines (79 loc) · 2.14 KB
/
WebsocketClient.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Require installing socket.io-client-simple
require 'socket.io-client-simple'
require 'base64'
require 'json'
$access_key = "<YOUR ACCESS KEY>"
$secret_key = "<YOUR SECRET KEY>"
def initial_post_data
post_data = {}
post_data['tonce'] = (Time.now.to_f * 1000000).to_i.to_s
post_data
end
def params_string(post_data)
post_data['params'] = post_data['params'].join(',')
params_parse(post_data).collect{|k, v| "#{k}=#{v}"} * '&'
end
def params_parse(post_data)
post_data['accesskey'] = $access_key #access key
post_data['requestmethod'] = 'post'
post_data['id'] = post_data['tonce'] unless post_data.keys.include?('id')
fields=['tonce','accesskey','requestmethod','id','method','params']
ordered_data = {}
fields.each do |field|
ordered_data[field] = post_data[field]
end
ordered_data
end
def sign(params_string)
signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), $secret_key, params_string) #secret key
Base64.strict_encode64($access_key+ ':' + signature)
end
def initial_post_data
post_data = {}
post_data['tonce'] = (Time.now.to_f * 1000000).to_i.to_s
post_data
end
socket = SocketIO::Client::Simple.connect 'https://websocket.btcchina.com'
socket.on:connect do
puts "connected!"
socket.emit :subscribe, "marketdata_cnybtc"
socket.emit :subscribe, "marketdata_cnyltc"
socket.emit :subscribe, "grouporder_cnybtc"
socket.emit :subscribe, "grouporder_cnyltc"
post_data = initial_post_data
post_data['method'] = 'subscribe'
post_data['params'] = ["order_cnybtc", "order_cnyltc", "account_info"]
payload = params_parse(post_data)
pstr = params_string(payload.clone)
signature_string = sign(pstr)
socket.emit :private, [payload.to_json, signature_string]
end
socket.on :disconnect do
puts "disconnected!"
end
socket.on :message do |data|
puts "message: "+data
end
socket.on :trade do |data|
puts 'trade:'
p data
end
socket.on :ticker do |data|
puts 'ticker:'
p data
end
socket.on :grouporder do |data|
puts 'grouporder:'
p data
end
socket.on :order do |data|
puts 'order:'
p data
end
socket.on :account_info do |data|
puts 'account_info:'
p data
end
loop do
sleep 3
end