mirror of
https://github.com/yulinfeng000/blive.git
synced 2024-12-26 12:50:22 +08:00
每个BLiver维护一个packman而不是共用,增加一个监听多个直播间的例子代码multi_room.py
This commit is contained in:
parent
6d9592a7fa
commit
88a09bb217
2
app.py
2
app.py
@ -37,7 +37,7 @@ async def listen_sc(ctx: BLiverCtx):
|
||||
msg = SuperChatMsg(ctx.body)
|
||||
print("sc 来了")
|
||||
print(
|
||||
f"""\n感谢 {msg.sender['name']}({msg.sender['medal']['medal_name']}:{msg.sender['medal']['medal_level']})的价值{msg.price}的sc\n\n\t{msg.content}\n"""
|
||||
f"\n感谢 {msg.sender['name']}({msg.sender['medal']['medal_name']}:{msg.sender['medal']['medal_level']})的价值 {msg.price} 的sc\n\n\t{msg.content}\n"
|
||||
)
|
||||
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
import asyncio
|
||||
from collections import namedtuple
|
||||
from multiprocessing import RawValue, Lock
|
||||
import json
|
||||
from random import randint, random
|
||||
from random import randint
|
||||
import requests
|
||||
import struct
|
||||
import enum
|
||||
|
@ -11,8 +11,8 @@ from apscheduler.util import _Undefined
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
from .core import (
|
||||
BWS_MsgPackage,
|
||||
PackageHeader,
|
||||
packman,
|
||||
Events,
|
||||
Operation,
|
||||
get_blive_room_info,
|
||||
@ -84,6 +84,7 @@ class BLiver:
|
||||
else:
|
||||
self.logger = logger
|
||||
self._ws: ClientWebSocketResponse = None
|
||||
self.packman = BWS_MsgPackage()
|
||||
self.scheduler = AsyncIOScheduler(timezone="Asia/ShangHai")
|
||||
self.processor = Processor(logger=self.logger)
|
||||
self.aio_session = aiohttp.ClientSession()
|
||||
@ -149,7 +150,7 @@ class BLiver:
|
||||
try:
|
||||
if self._ws is not None and not self._ws.closed:
|
||||
await self._ws.send_bytes(
|
||||
packman.pack(heartbeat(), Operation.HEARTBEAT)
|
||||
self.packman.pack(heartbeat(), Operation.HEARTBEAT)
|
||||
)
|
||||
self.logger.debug("heartbeat sended")
|
||||
return
|
||||
@ -173,7 +174,9 @@ class BLiver:
|
||||
self._ws = ws
|
||||
# 发送认证
|
||||
await ws.send_bytes(
|
||||
packman.pack(certification(self.real_roomid, token), Operation.AUTH)
|
||||
self.packman.pack(
|
||||
certification(self.real_roomid, token), Operation.AUTH
|
||||
)
|
||||
)
|
||||
return
|
||||
except (
|
||||
@ -209,7 +212,7 @@ class BLiver:
|
||||
continue
|
||||
if msg.type != aiohttp.WSMsgType.BINARY:
|
||||
continue
|
||||
mq = packman.unpack(msg.data)
|
||||
mq = self.packman.unpack(msg.data)
|
||||
self.logger.debug("received msg:\n{}", mq)
|
||||
tasks = [self.processor.process(BLiverCtx(self, m)) for m in mq]
|
||||
await asyncio.gather(*tasks)
|
||||
|
@ -1,6 +1,5 @@
|
||||
from abc import ABC
|
||||
import json
|
||||
from re import L
|
||||
from typing import List
|
||||
|
||||
"""
|
||||
|
35
multi_room.py
Normal file
35
multi_room.py
Normal file
@ -0,0 +1,35 @@
|
||||
"""监听多个直播间的例子"""
|
||||
|
||||
import asyncio
|
||||
from blive import BLiver, Events, BLiverCtx
|
||||
from blive.msg import DanMuMsg
|
||||
|
||||
|
||||
# 定义弹幕事件handler
|
||||
async def listen(ctx: BLiverCtx):
|
||||
danmu = DanMuMsg(ctx.body)
|
||||
print(
|
||||
f'\n{danmu.sender.name} ({danmu.sender.medal.medal_name}:{danmu.sender.medal.medal_level}): "{danmu.content}"\n'
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
# 两个直播间
|
||||
ke = BLiver(605)
|
||||
azi = BLiver(510)
|
||||
|
||||
# 注册handler
|
||||
ke.register_handler(Events.DANMU_MSG, listen)
|
||||
azi.register_handler(Events.DANMU_MSG, listen)
|
||||
|
||||
# 以异步task的形式运行
|
||||
task1 = ke.run_as_task()
|
||||
task2 = azi.run_as_task()
|
||||
|
||||
# await 两个任务
|
||||
await asyncio.gather(*[task1, task2])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(main())
|
Loading…
Reference in New Issue
Block a user