forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinace_socket_manager.py
More file actions
34 lines (27 loc) · 904 Bytes
/
Copy pathbinace_socket_manager.py
File metadata and controls
34 lines (27 loc) · 904 Bytes
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
import os
import sys
import asyncio
import time
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(root)
from binance import AsyncClient, BinanceSocketManager
async def main():
client = await AsyncClient.create()
bm = BinanceSocketManager(client)
# start any sockets here, i.e a trade socket
ts = bm.trade_socket("BTCUSDT")
# then start receiving messages
async with ts as tscm:
start_time = time.time()
while time.time() - start_time < 30:
try:
res = await tscm.recv()
print(res)
except Exception as e:
print(f"An error occurred: {e}")
break
await client.close_connection()
print("WebSocket connection closed after 10 seconds.")
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())