-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb-socket.py
More file actions
95 lines (79 loc) · 2.21 KB
/
web-socket.py
File metadata and controls
95 lines (79 loc) · 2.21 KB
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
import json
import websocket
import global_constant
import my_functions
# def ws_open():
# ws_query = '{}'.format(websocket_base_url)
# ws = websocket.WebSocket()
# ws.connect(ws_query)
# return ws
#
#
# def ws_send_heartbeat():
# ws = ws_open()
# params = {
# 'op': 'ping'
# }
# params_string = json.dumps(params)
# ws.send(params_string)
# ret_res = ws.recv()
# ws.close()
# return ret_res
#
#
# def ws_order_book25(symbol):
# ws = ws_open()
# params = {
# 'op': 'subscribe',
# 'args': [
# 'orderBook25.{}'.format(symbol)
# ]
# }
# params_string = json.dumps(params)
# ws.send(params_string)
# ret_res = ws.recv()
# ws.close()
# return ret_res
#
#
# def ws_kline(symbol='*', interval='*'):
# ws = ws_open()
# params = {
# 'op': 'subscribe',
# 'args': [
# 'kline.{}.{}'.format(symbol, interval)
# ]
# }
# params_string = json.dumps(params)
# print('send', params_string)
# ws.send(params_string)
# ret_res = ws.recv()
# ws.close()
# return ret_res
def on_ws_message(ws, message):
my_functions.eprint('message', message)
def on_ws_error(ws, error):
my_functions.eprint('error', error)
def on_ws_close(ws):
my_functions.eprint("### closed ###")
def on_ws_open(ws):
my_functions.eprint("### opened ###")
expires = my_functions.get_current_timestamp()
signature_param = 'GET/realtime{}'.format(expires)
signature = my_functions.generate_hmac_sha256_hex(global_constant.secret_key, signature_param)
params = {
'op': 'auth',
'args': [global_constant.api_key, expires, signature]
}
ws_query = json.dumps(params)
ws.send(ws_query)
# ws.send('{"op":"subscribe","args":["position"]}')
ws.send('{"op": "subscribe", "args": ["orderBook25.BTCUSD"]}')
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp(global_constant.websocket_base_url,
on_message=on_ws_message,
on_error=on_ws_error,
on_close=on_ws_close)
ws.on_open = on_ws_open
ws.run_forever()