fix(pterodactyl): handle disconnecting from the websocket
This commit is contained in:
parent
e6ca4071f3
commit
d391aeeb7b
1 changed files with 23 additions and 18 deletions
|
@ -47,27 +47,32 @@ class Pterodactyl(commands.Cog):
|
||||||
self.logger.error('Failed to retrieve Pterodactyl websocket: %s', e)
|
self.logger.error('Failed to retrieve Pterodactyl websocket: %s', e)
|
||||||
return
|
return
|
||||||
|
|
||||||
async with websockets.connect(websocket_credentials['data']['socket'], extra_headers=extra_headers) as websocket:
|
async for websocket in websockets.connect(websocket_credentials['data']['socket'], extra_headers=extra_headers):
|
||||||
self.logger.debug("WebSocket connection established")
|
try:
|
||||||
|
self.logger.debug("WebSocket connection established")
|
||||||
|
|
||||||
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
||||||
await websocket.send(auth_message)
|
await websocket.send(auth_message)
|
||||||
self.logger.debug("Authentication message sent")
|
self.logger.debug("Authentication message sent")
|
||||||
|
|
||||||
self.client = client
|
self.client = client
|
||||||
self.websocket = websocket
|
self.websocket = websocket
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
message = await websocket.recv()
|
message = await websocket.recv()
|
||||||
if json.loads(message)['event'] in ['token expiring', 'token expired']:
|
if json.loads(message)['event'] in ['token expiring', 'token expired']:
|
||||||
self.logger.debug("Received token expiring/expired event. Refreshing token.")
|
self.logger.debug("Received token expiring/expired event. Refreshing token.")
|
||||||
websocket_credentials = client.servers.get_websocket(server_id)
|
websocket_credentials = client.servers.get_websocket(server_id)
|
||||||
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
||||||
await websocket.send(auth_message)
|
await websocket.send(auth_message)
|
||||||
self.logger.debug("Authentication message sent")
|
self.logger.debug("Authentication message sent")
|
||||||
if json.loads(message)['event'] == 'auth success':
|
if json.loads(message)['event'] == 'auth success':
|
||||||
self.logger.debug("Authentication successful")
|
self.logger.debug("Authentication successful")
|
||||||
self.logger.debug("Received message: %s", message)
|
self.logger.debug("Received message: %s", message)
|
||||||
|
except websockets.exceptions.ConnectionClosed as e:
|
||||||
|
self.logger.debug("WebSocket connection closed: %s", e)
|
||||||
|
websocket_credentials = client.servers.get_websocket(server_id)
|
||||||
|
continue
|
||||||
|
|
||||||
def get_task(self):
|
def get_task(self):
|
||||||
return self.bot.loop.create_task(self.establish_websocket_connection(), name="Pterodactyl Websocket Connection")
|
return self.bot.loop.create_task(self.establish_websocket_connection(), name="Pterodactyl Websocket Connection")
|
||||||
|
|
Loading…
Reference in a new issue