-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket_client.py
81 lines (64 loc) · 2.45 KB
/
websocket_client.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
import asyncio
import json
import websockets
import websockets.exceptions
import os
import logging
from concurrent.futures import ThreadPoolExecutor
from dotenv import load_dotenv
from pydantic import BaseModel
load_dotenv()
# Demo user (may not work in production)
class DemoUser(BaseModel):
api_key: str = "xxx"
api_secret: str = "xxx"
# Creating a ThreadPoolExecutor for running input in a separate thread
executor = ThreadPoolExecutor()
async def get_input(prompt):
loop = asyncio.get_running_loop()
return await loop.run_in_executor(executor, input, prompt)
async def send_messages(websocket):
while True:
message = await get_input("")
try:
message_json = {
"message": message,
"timestamp": None,
"target_user_id": None,
}
await websocket.send(json.dumps(message_json))
print("Message sent: ", message)
except websockets.exceptions.ConnectionClosedError:
print("Server is not available. Exiting...")
break
async def receive_messages(websocket):
while True:
response = await websocket.recv()
print("Received: ", response)
async def websocket_client():
API_KEY = os.getenv("API_KEY")
API_SECRET = os.getenv("API_SECRET")
uri = "wss://api.princeofcrypto.com/v1/stream/signals"
if API_KEY is None or API_SECRET is None:
logging.warning(
"API_KEY or API_SECRET not found in environment variables. Using demo user."
)
demo_user = DemoUser()
API_KEY = demo_user.api_key
API_SECRET = demo_user.api_secret
headers = {"x-api-key": API_KEY, "x-api-secret": API_SECRET}
async def async_websocket_connection(uri, headers):
async with websockets.connect(uri, extra_headers=headers) as websocket:
sender_task = asyncio.create_task(send_messages(websocket))
receiver_task = asyncio.create_task(receive_messages(websocket))
await asyncio.gather(sender_task, receiver_task)
# Connecting with headers
while True:
try:
await async_websocket_connection(uri, headers)
except websockets.exceptions.ConnectionClosedError:
print("Connection closed. Reconnecting in 5 sec...")
await asyncio.sleep(5)
if __name__ == "__main__":
asyncio.run(websocket_client())
# websockets.exceptions.ConnectionClosedError -> asyncio.exceptions.IncompleteReadError