移除了一堆没用的特性

消息处理接口改成同步函数
消息处理器从数组改成单个对象
移除web端短ID属性
移除SSL配置
消息处理器添加客户端异常停止的回调
This commit is contained in:
John Smith
2023-09-03 18:09:12 +08:00
parent 74b9cdc100
commit 300840fd65
6 changed files with 75 additions and 103 deletions

View File

@@ -18,6 +18,9 @@ TEST_ROOM_IDS = [
23105590,
]
# 这里填一个已登录账号的cookie。不填cookie也可以连接但是收到弹幕的用户名会打码UID会变成0
SESSDATA = ''
session: Optional[aiohttp.ClientSession] = None
@@ -31,9 +34,8 @@ async def main():
def init_session():
# 这里填一个已登录账号的cookie。不填cookie也可以连接但是收到弹幕的用户名会打码UID会变成0
cookies = http.cookies.SimpleCookie()
cookies['SESSDATA'] = ''
cookies['SESSDATA'] = SESSDATA
cookies['SESSDATA']['domain'] = 'bilibili.com'
global session
@@ -46,10 +48,9 @@ async def run_single_client():
演示监听一个直播间
"""
room_id = random.choice(TEST_ROOM_IDS)
# 如果SSL验证失败就把ssl设为FalseB站真的有过忘续证书的情况
client = blivedm.BLiveClient(room_id, session=session, ssl=True)
client = blivedm.BLiveClient(room_id, session=session)
handler = MyHandler()
client.add_handler(handler)
client.set_handler(handler)
client.start()
try:
@@ -69,7 +70,7 @@ async def run_multi_clients():
clients = [blivedm.BLiveClient(room_id, session=session) for room_id in TEST_ROOM_IDS]
handler = MyHandler()
for client in clients:
client.add_handler(handler)
client.set_handler(handler)
client.start()
try:
@@ -87,25 +88,25 @@ class MyHandler(blivedm.BaseHandler):
# _CMD_CALLBACK_DICT = blivedm.BaseHandler._CMD_CALLBACK_DICT.copy()
#
# # 入场消息回调
# async def __interact_word_callback(self, client: blivedm.BLiveClient, command: dict):
# def __interact_word_callback(self, client: blivedm.BLiveClient, command: dict):
# print(f"[{client.room_id}] INTERACT_WORD: self_type={type(self).__name__}, room_id={client.room_id},"
# f" uname={command['data']['uname']}")
# _CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback # noqa
async def _on_heartbeat(self, client: blivedm.BLiveClient, message: web_models.HeartbeatMessage):
def _on_heartbeat(self, client: blivedm.BLiveClient, message: web_models.HeartbeatMessage):
print(f'[{client.room_id}] 心跳')
async def _on_danmaku(self, client: blivedm.BLiveClient, message: web_models.DanmakuMessage):
def _on_danmaku(self, client: blivedm.BLiveClient, message: web_models.DanmakuMessage):
print(f'[{client.room_id}] {message.uname}{message.msg}')
async def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage):
def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage):
print(f'[{client.room_id}] {message.uname} 赠送{message.gift_name}x{message.num}'
f' {message.coin_type}瓜子x{message.total_coin}')
async def _on_buy_guard(self, client: blivedm.BLiveClient, message: web_models.GuardBuyMessage):
def _on_buy_guard(self, client: blivedm.BLiveClient, message: web_models.GuardBuyMessage):
print(f'[{client.room_id}] {message.username} 购买{message.gift_name}')
async def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperChatMessage):
def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperChatMessage):
print(f'[{client.room_id}] 醒目留言 ¥{message.price} {message.uname}{message.message}')