Attempting to resubscribe/reconnect
-
Hi Moderator,
I used the below code from the github and getting the below error. Please advise
W 240404 23:07:05 smartWebSocketV2:319]
Attempting to resubscribe/reconnect (Attempt 1)...
Attempting to resubscribe/reconnect (Attempt 2)...
[E 240404 23:07:25 265206087:58] Connection closed
[W 240404 23:07:25 smartWebSocketV2:343] Connection closed due to max retry attempts reached.For your reference, please find the code below:
from SmartApi.smartWebSocketV2 import SmartWebSocketV2
from logzero import logger
from SmartApi import SmartConnect #or from SmartApi.smartConnect import SmartConnect
from logzero import logger
import pyotpcorrelation_id = "abc123"
action = 1
mode = 1
AUTH_TOKEN = "Enter auth token here"
API_KEY = "Enter API Key here"
CLIENT_CODE = "S1003394"
correlation_id = "abc123"
pwd="Enter Pin here"
action = 1
mode = 1
smartApi = SmartConnect(API_KEY)
totp = pyotp.TOTP(AUTH_TOKEN).now()
FEED_TOKEN=smartApi.generateSession(CLIENT_CODE, pwd, totp)token_list = [
{
"exchangeType": 1,
"tokens": ["39011"]
}
]
#retry_strategy=0 for simple retry mechanism
sws = SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN,max_retry_attempt=2)#retry_strategy=1 for exponential retry mechanism
sws = SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN,max_retry_attempt=3, retry_strategy=1, retry_delay=10,retry_multiplier=2, retry_duration=30)
def on_data(wsapp, message):
logger.info("Ticks: {}".format(message))
# close_connection()def on_control_message(wsapp, message):
logger.info(f"Control Message: {message}")def on_open(wsapp):
logger.info("on open")
some_error_condition = False
if some_error_condition:
error_message = "Simulated error"
if hasattr(wsapp, 'on_error'):
wsapp.on_error("Custom Error Type", error_message)
else:
sws.subscribe(correlation_id, mode, token_list)
# sws.unsubscribe(correlation_id, mode, token_list1)def on_error(wsapp, error):
logger.error(error)def on_close(wsapp):
logger.info("Close")def close_connection():
sws.close_connection()Assign the callbacks.
sws.on_open = on_open
sws.on_data = on_data
sws.on_error = on_error
sws.on_close = on_close
sws.on_control_message = on_control_messagesws.connect()
from SmartApi.smartWebSocketV2 import SmartWebSocketV2
from logzero import logger
from SmartApi import SmartConnect #or from SmartApi.smartConnect import SmartConnect
from logzero import logger
import pyotpcorrelation_id = "abc123"
action = 1
mode = 1
AUTH_TOKEN = "Enter auth token here"
API_KEY = "Enter API Key here"
CLIENT_CODE = "S1003394"
correlation_id = "abc123"
pwd="Enter Pin here"
action = 1
mode = 1
smartApi = SmartConnect(API_KEY)
totp = pyotp.TOTP(AUTH_TOKEN).now()
FEED_TOKEN=smartApi.generateSession(CLIENT_CODE, pwd, totp)token_list = [
{
"exchangeType": 1,
"tokens": ["39011"]
}
]
#retry_strategy=0 for simple retry mechanism
sws = SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN,max_retry_attempt=2)#retry_strategy=1 for exponential retry mechanism
sws = SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN,max_retry_attempt=3, retry_strategy=1, retry_delay=10,retry_multiplier=2, retry_duration=30)
def on_data(wsapp, message):
logger.info("Ticks: {}".format(message))
# close_connection()def on_control_message(wsapp, message):
logger.info(f"Control Message: {message}")def on_open(wsapp):
logger.info("on open")
some_error_condition = False
if some_error_condition:
error_message = "Simulated error"
if hasattr(wsapp, 'on_error'):
wsapp.on_error("Custom Error Type", error_message)
else:
sws.subscribe(correlation_id, mode, token_list)
# sws.unsubscribe(correlation_id, mode, token_list1)def on_error(wsapp, error):
logger.error(error)def on_close(wsapp):
logger.info("Close")def close_connection():
sws.close_connection()Assign the callbacks.
sws.on_open = on_open
sws.on_data = on_data
sws.on_error = on_error
sws.on_close = on_close
sws.on_control_message = on_control_messagesws.connect()