Skip to content

Commit

Permalink
correções e pequenas melhorias...
Browse files Browse the repository at this point in the history
  • Loading branch information
zRitsu committed Dec 18, 2021
1 parent d128fa2 commit 958b660
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def update(self, user_id: int, bot_id: int, data: dict):

self.check_presence(user_id, bot_id)

if not data:
self.users_rpc[user_id][bot_id].clear()
return

payload = {
"assets": {
"large_image": "app"
Expand All @@ -133,6 +137,8 @@ def update(self, user_id: int, bot_id: int, data: dict):

info = data.pop("info", None)

payload.update(data)

if info and track:

m = info["members"]
Expand Down Expand Up @@ -252,11 +258,7 @@ def update(self, user_id: int, bot_id: int, data: dict):
if buttons:
payload["buttons"] = buttons

self.users_rpc[user_id][bot_id].update_activity(payload)

else:
self.users_rpc[user_id][bot_id].clear()
return
self.users_rpc[user_id][bot_id].update_activity(payload)


def get_lang(self, key: str) -> str:
Expand All @@ -278,6 +280,8 @@ async def handle_socket(self, uri):
try:
async with websockets.connect(uri) as ws:

print(f"Websocket conectado: {uri}")

for i in user_clients:
await ws.send(json.dumps({"user_id": i}))

Expand All @@ -295,7 +299,7 @@ async def handle_socket(self, uri):

op = data.pop("op")
public = data.pop("public", True)
bot_id = data.get("bot_id")
bot_id = data.pop("bot_id", None)
user = user_clients[user_id]["user"]

print(f"op: {op} | {user} [{user_id}] | bot: {bot_id}")
Expand Down Expand Up @@ -341,6 +345,7 @@ async def handle_socket(self, uri):
self.get_lang("listeners") + f': {m}'
except KeyError:
pass

self.update(user_id, bot_id, data)

case "close":
Expand All @@ -350,15 +355,16 @@ async def handle_socket(self, uri):
print(f"unknow op: {msg.data}")


except (websockets.ConnectionClosedError, ConnectionRefusedError, ConnectionResetError, OSError):
print(f"Conexão perdida com o servidor: {uri} | Reconectando em 60seg.")
except (websockets.ConnectionClosedError, ConnectionResetError) as e:
print(f"Conexão perdida com o servidor: {uri} | Reconectando em 60seg. {repr(e)}")
await asyncio.sleep(60)
except ConnectionRefusedError:
await asyncio.sleep(500)
except Exception as e:
traceback.print_exc()
print(f"Erro na conexão: {uri} | {repr(e)}")
await asyncio.sleep(60)

async def handler(self):
await asyncio.wait([asyncio.create_task(self.handle_socket(uri)) for uri in config["urls"]])

asyncio.get_event_loop().run_until_complete(RpcClient().handler())
asyncio.run(RpcClient().handler())

0 comments on commit 958b660

Please sign in to comment.