It looks like you're new here. If you want to get involved, click one of these buttons!
def on_close(ws, code, reason):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
if current_time > '15:30:01':
ws.stop()
sys.exit()
else:
success = False
while not success:
success = reconnect_to_websocket(kws)
print(success)
if not success:
time.sleep(30)
def reconnect_to_websocket(kws, timeout=30):
start_time = time.time()
feed2 = False
while not feed2:
if check_internet_connection():
kws.connect(threaded=True)
while not kws.is_connected():
if time.time() - start_time > timeout:
# indicate the failure to connect to the WebSocket
return False
time.sleep(1)
feed2 = True
else:
time.sleep(5)
return True
PS: Default
reconnect_max_tries
attempt is 50. You can assign an increased value if you feel the disconnection time could be more on your end. The code comment here explains more.1. Even if it says the default is 50, If I change the default to the maximum allowed (300), it still works correctly?
2. Also I've tested if the default value is exhausted and no reconnect hits, still if I try to make the kws.connect() in the same code (after a certain time).. it still connects and subscriptions are still valid.
Note: I've tested at my side by disconnecting the wifi
Now, my point is if Zerodha from their side resets the WebSocket..due to some API issue..how can I check now, if the tokens are still subscribed. for example, what will ticks return if I reconnect the WebSocket again without getting the actual data?
kws.connect()
in the main thread, it's an infinite loop.