Skip to content

Commit

Permalink
feat: using logging #3
Browse files Browse the repository at this point in the history
feat: using logging
  • Loading branch information
amakerlife authored Feb 14, 2024
2 parents 49799d1 + 05a3158 commit 1d0ef6f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pyinstaller-hooks-contrib==2023.11
pywin32-ctypes==0.2.2
websocket-client==1.7.0
win11toast==0.34
winsdk==1.0.0b10
winsdk==1.0.0b10

0 comments on commit 1d0ef6f

Please sign in to comment.