forked from mechnotech/coinex-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_runner.py
98 lines (80 loc) · 2.22 KB
/
socket_runner.py
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
98
import json
from client import Api
soc_url = 'wss://socket.coinex.com/'
mess = {
"id": 4,
"method": "state.subscribe",
"params": []
}
message = json.dumps(mess).encode('utf-8')
# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.connect((soc_url, 8765))
# s.send(message)
# print(s)
# s.close()
import asyncio
import websockets
coinex = Api()
data = {'access_id': coinex.access_id, 'tonce': coinex.ts}
sign = coinex._sign(data)
auth_soc = {
"method": "server.sign",
"params": [
coinex.access_id,
sign,
coinex.ts
],
"id": 15,
}
subscribe_soc = {
"method": "depth.subscribe",
"params": ["EMCUSDT", 1, '0'],
"id": 114775
}
orders_soc = {
"method": "order.account_query",
"params": [0, "EMCUSDT", 0, 0, 10],
"id": 65744
}
async def pinger(websocket):
ping_soc = {
"method": "server.ping",
"params": [],
"id": 11
}
await asyncio.sleep(2)
await websocket.send(json.dumps(ping_soc).encode('utf-8'))
async def connect_soc():
async with websockets.connect(soc_url, ping_interval=10, ping_timeout=None) as websocket:
# await websocket.send(message)
# print(f"> {message}")
# greeting = await websocket.recv()
# print(f"< {greeting}")
await websocket.send(json.dumps(auth_soc).encode('utf-8'))
await pinger(websocket)
print('try sign')
await websocket.recv()
# print('result of auth', res)
await websocket.send(json.dumps(subscribe_soc).encode('utf-8'))
await websocket.send(json.dumps(orders_soc).encode('utf-8'))
cnt = 0
async for msg in websocket:
print(msg)
await pinger(websocket)
# while True:
# res = await asyncio.wait_for(websocket.recv(), timeout=10)
# print(res)
# if cnt > 5:
# print('cnt = 5')
# await websocket.ping()
# cnt = 0
# cnt += 1
# # await pinger(websocket)
async def main_loop():
try:
await connect_soc()
except Exception as e:
print(e)
return
asyncio.get_event_loop().run_until_complete(main_loop())
# asyncio.get_event_loop().run_forever()