Skip to content
This repository was archived by the owner on Apr 26, 2026. It is now read-only.

Commit b6f2a3a

Browse files
committed
update
1 parent 4957615 commit b6f2a3a

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

719 Bytes
Binary file not shown.

pocketoptionapi/ws/client.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ def __init__(self, api) -> None:
5959
self.esperar_segundo_mensaje = False
6060
self.recibido_updateClosedDeals = False
6161

62+
async def reconnect(self):
63+
regs = self.region.get_regions()
64+
for i in regs:
65+
print(f"Reconnecting to {i}...")
66+
async with websockets.connect(i, extra_headers={"Origin": "https://m.pocketoption.com"}) as ws:
67+
68+
print("Conectado a: ", i)
69+
self.websocket = ws
70+
self.url = i
71+
on_message_task = asyncio.create_task(self.websocket_listener(ws))
72+
sender_task = asyncio.create_task(send_pin(ws))
73+
message_task = asyncio.create_task(self.send_message(self.message))
74+
await asyncio.gather(on_message_task, sender_task, message_task)
75+
76+
6277
async def websocket_listener(self, ws):
6378
async for message in ws:
6479
await self.on_message(message)
@@ -139,6 +154,15 @@ async def on_message(self, message): # pylint: disable=unused-argument
139154
global_value.balance = message["balance"]
140155
global_value.balance_type = message["isDemo"]
141156

157+
data = {
158+
"balance_id" : message["uid"],
159+
"balance" : message["balance"],
160+
"balance_type" : message["isDemo"]
161+
}
162+
163+
with open("balance.json", "w") as f:
164+
json.dump(data, f)
165+
142166
elif "requestId" in message and message["requestId"] == 'buy':
143167
global_value.order_data = message
144168

@@ -220,11 +244,17 @@ async def on_error(self, error): # pylint: disable=unused-argument
220244
"""Method to process websocket errors."""
221245
logger = logging.getLogger(__name__)
222246
logger.error(error)
223-
global_value.websocket_error_reason = str(error)
224-
global_value.check_websocket_if_error = True
247+
try:
248+
self.reconnect()
249+
except:
250+
global_value.websocket_error_reason = str(error)
251+
global_value.check_websocket_if_error = True
225252

226253
async def on_close(self, error): # pylint: disable=unused-argument
227254
"""Method to process websocket close."""
228255
logger = logging.getLogger(__name__)
229256
logger.debug("Websocket connection closed.")
230-
global_value.websocket_is_connected = False
257+
try:
258+
self.reconnect()
259+
except:
260+
global_value.websocket_is_connected = False

test.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
api = PocketOption(ssid)
99
print(ssid)
1010

11-
1211
def direction():
1312
# Selecciona aleatoriamente entre 'call' y 'put'
1413
return random.choice(['call', 'put'])
1514

16-
1715
if __name__ == "__main__":
1816
api.connect()
1917
time.sleep(2)
20-
print(f"Get balance: {api.get_balance()}")

0 commit comments

Comments
 (0)