Readding websocket reconnection functions
This commit is contained in:
parent
4cb07c2647
commit
c344777f22
@ -2213,6 +2213,7 @@ def scanBlockchain():
|
|||||||
# At this point the script has updated to the latest block
|
# At this point the script has updated to the latest block
|
||||||
# Now we connect to flosight's websocket API to get information about the latest blocks
|
# Now we connect to flosight's websocket API to get information about the latest blocks
|
||||||
|
|
||||||
|
|
||||||
# Configuration of required variables
|
# Configuration of required variables
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
@ -2220,8 +2221,7 @@ logger.setLevel(logging.DEBUG)
|
|||||||
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
|
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
|
||||||
|
|
||||||
file_handler = logging.FileHandler('tracking.log')
|
file_handler = logging.FileHandler('tracking.log')
|
||||||
file_handler.setLevel(logging.INFO)
|
file_handler.setLevel(logging.INFO)file_handler.setFormatter(formatter)
|
||||||
file_handler.setFormatter(formatter)
|
|
||||||
|
|
||||||
stream_handler = logging.StreamHandler()
|
stream_handler = logging.StreamHandler()
|
||||||
stream_handler.setFormatter(formatter)
|
stream_handler.setFormatter(formatter)
|
||||||
@ -2302,22 +2302,60 @@ if args.reset == 1:
|
|||||||
# MAIN LOGIC
|
# MAIN LOGIC
|
||||||
# scan from the latest block saved locally to latest network block
|
# scan from the latest block saved locally to latest network block
|
||||||
scanBlockchain()
|
scanBlockchain()
|
||||||
|
scanBlockchain()
|
||||||
|
|
||||||
# Connect to Flosight websocket to get data on new incoming blocks
|
def switchNeturl(neturl):
|
||||||
sio = socketio.Client(reconnection=True)
|
testserverlist = ['http://0.0.0.0:9000/', 'https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
||||||
sio.connect(neturl + "socket.io/socket.io.js")
|
mainserverlist = ['http://0.0.0.0:9001/', 'https://flosight.duckdns.org/', 'https://explorer.mediciland.com/', 'https://livenet.flocha.in/']
|
||||||
|
if config['DEFAULT']['NET'] == 'mainnet':
|
||||||
|
neturlindex = mainserverlist.index(neturl)
|
||||||
|
if neturlindex+1 >= len(mainserverlist):
|
||||||
|
return mainserverlist[neturlindex+1 - len(mainserverlist)]
|
||||||
|
else:
|
||||||
|
return mainserverlist[neturlindex+1]
|
||||||
|
elif config['DEFAULT']['NET'] == 'testnet':
|
||||||
|
neturlindex = testserverlist.index(neturl)
|
||||||
|
if neturlindex+1 >= len(testserverlist):
|
||||||
|
return testserverlist[neturlindex+1 - len(testserverlist)]
|
||||||
|
else:
|
||||||
|
return testserverlist[neturlindex+1]
|
||||||
|
|
||||||
@sio.on('connect')
|
def reconnectSSE(neturl):
|
||||||
def on_connect():
|
|
||||||
logger.debug('Token Tracker has connected to websocket')
|
|
||||||
sio.emit('subscribe', 'inv')
|
|
||||||
|
|
||||||
@sio.on('disconnect')
|
# Connect to Flosight websocket to get data on new incoming blocks
|
||||||
def disconnect():
|
sio = socketio.Client(reconnection=False)
|
||||||
logger.debug('Token Tracker disconnected from websocket')
|
try:
|
||||||
logger.debug('The script will rescan from the latest block in local db to latest server on FLO network')
|
sio.connect( neturl + "socket.io/socket.io.js")
|
||||||
scanBlockchain()
|
except:
|
||||||
logger.debug('Attempting reconnect to websocket ...')
|
logger.debug(f"Could not connect to the websocket endpoint {neturl}. Switching & retrying to next")
|
||||||
|
neturl = switchNeturl(neturl)
|
||||||
|
reconnectSSE(neturl)
|
||||||
|
|
||||||
|
@sio.on('connect')
|
||||||
|
def on_connect():
|
||||||
|
logger.debug(f"Token Tracker has connected to websocket endpoint {neturl}")
|
||||||
|
sio.emit('subscribe', 'inv')
|
||||||
|
|
||||||
|
@sio.on('disconnect')
|
||||||
|
def disconnect():
|
||||||
|
logger.debug(f"Token Tracker disconnected from websocket endpoint {neturl}")
|
||||||
|
logger.debug('The script will rescan from the latest block in local db to latest server on FLO network')
|
||||||
|
scanBlockchain()
|
||||||
|
logger.debug("Rescan completed")
|
||||||
|
logger.debug('Attempting reconnect to websocket ...')
|
||||||
|
reconnectSSE(neturl)
|
||||||
|
|
||||||
|
@sio.on('connect_error')
|
||||||
|
def connect_error():
|
||||||
|
logger.debug(f"CONNECTION_ERROR to the websocket endpoint {neturl}. Switching & retrying to next")
|
||||||
|
neturl = switchNeturl(neturl)
|
||||||
|
reconnectSSE(neturl)
|
||||||
|
|
||||||
|
@sio.on('block')
|
||||||
|
def on_block(data):
|
||||||
|
logger.debug('New block received')
|
||||||
|
logger.debug(str(data))
|
||||||
|
processApiBlock(data)
|
||||||
|
|
||||||
# At this point the script has updated to the latest block
|
# At this point the script has updated to the latest block
|
||||||
# Now we connect to flosight's websocket API to get information about the latest blocks
|
# Now we connect to flosight's websocket API to get information about the latest blocks
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user