def on_close(ws, code, reason):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
if current_time > '15:30:01':
success = False
while not success:
success = reconnect_to_websocket(kws)
if not success:
def reconnect_to_websocket(kws, timeout=30):
start_time = time.time()
feed2 = False
while not feed2:
while not kws.is_connected():
if time.time() - start_time > timeout:
# indicate the failure to connect to the WebSocket
feed2 = True
reconnect_max_triesattempt 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.