Skip to content

Commit

Permalink
feat: using logging
Browse files Browse the repository at this point in the history
  • Loading branch information
amakerlife committed Feb 14, 2024
1 parent 68c1a7b commit 84524e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
20 changes: 10 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import json
import time
import websocket
from win11toast import toast
import time
import logging

logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s: %(message)s')

with open("cookie.txt", "r") as file:
_uid, __client_id = file.read().strip().split()
Expand All @@ -14,13 +18,9 @@
MAX_RECONNECTS = 5


def print_msg(msg):
print(f'{int(time.time() - st)} {msg}')


def on_open(ws):
global cnt_reconnect
print_msg("连接成功")
logging.info("连接成功")
cnt_reconnect = 0
data = json.dumps({
"channel": "chat",
Expand All @@ -31,14 +31,14 @@ def on_open(ws):


def on_close(ws, close_status_code, close_msg):
print_msg("连接已被关闭")
logging.warning("连接已被关闭")


def on_message(ws, message):
data = json.loads(message)
if data.get("_ws_type") == "server_broadcast":
msg = data["message"]
print_msg(f'{msg["sender"]["name"]}{msg["receiver"]["name"]}: {msg["content"]}')
logging.info(f'{msg["sender"]["name"]}{msg["receiver"]["name"]}: {msg["content"]}')
if str(msg["sender"]["uid"]) != str(_uid):
button_open = {
"activationType": "protocol",
Expand Down Expand Up @@ -68,10 +68,10 @@ def connect():
pass
cnt_reconnect += 1
# print_msg("连接已被关闭")
print_msg(f'正在尝试重连({cnt_reconnect}/{MAX_RECONNECTS})')
logging.info(f'正在尝试重连({cnt_reconnect}/{MAX_RECONNECTS})')
time.sleep(5)
if cnt_reconnect >= MAX_RECONNECTS:
print_msg("连接超时")
logging.error("连接超时")
toast("连接超时")
break

Expand Down
13 changes: 9 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
altgraph==0.17.4
cffi==1.16.0
gevent==23.9.1
greenlet==3.0.3
packaging==23.2
pefile==2023.2.7
pyinstall==0.1.4
pyinstaller==6.3.0
pyinstaller-hooks-contrib==2023.11
pycparser==2.21
pyinstaller==6.4.0
pyinstaller-hooks-contrib==2024.1
pywin32-ctypes==0.2.2
websocket-client==1.7.0
websocket==0.2.1
win11toast==0.34
winsdk==1.0.0b10
zope.event==5.0
zope.interface==6.1

0 comments on commit 84524e7

Please sign in to comment.