mirror of
https://github.com/xfgryujk/blivedm.git
synced 2025-02-06 09:40:49 +08:00
添加掉线重连
This commit is contained in:
parent
203f503ecc
commit
600038b683
72
blivedm.py
72
blivedm.py
@ -8,6 +8,7 @@ from enum import IntEnum
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
import websockets
|
import websockets
|
||||||
|
from websockets.exceptions import ConnectionClosed
|
||||||
|
|
||||||
|
|
||||||
class Operation(IntEnum):
|
class Operation(IntEnum):
|
||||||
@ -47,6 +48,8 @@ class BLiveClient:
|
|||||||
else:
|
else:
|
||||||
self._room_id = res.json()['data']['room_id']
|
self._room_id = res.json()['data']['room_id']
|
||||||
|
|
||||||
|
if self._future is not None:
|
||||||
|
return
|
||||||
self._future = gather(
|
self._future = gather(
|
||||||
self._message_loop(),
|
self._message_loop(),
|
||||||
self._heartbeat_loop()
|
self._heartbeat_loop()
|
||||||
@ -71,19 +74,27 @@ class BLiveClient:
|
|||||||
return header + body
|
return header + body
|
||||||
|
|
||||||
async def _message_loop(self):
|
async def _message_loop(self):
|
||||||
try:
|
while True:
|
||||||
# 连接
|
try:
|
||||||
async with websockets.connect(self.WEBSOCKET_URL) as websocket:
|
# 连接
|
||||||
self._websocket = websocket
|
async with websockets.connect(self.WEBSOCKET_URL) as websocket:
|
||||||
await self._send_auth()
|
self._websocket = websocket
|
||||||
|
await self._send_auth()
|
||||||
|
|
||||||
# 处理消息
|
# 处理消息
|
||||||
async for message in websocket:
|
async for message in websocket:
|
||||||
await self._handle_message(message)
|
await self._handle_message(message)
|
||||||
except CancelledError:
|
|
||||||
pass
|
except CancelledError:
|
||||||
finally:
|
break
|
||||||
self._websocket = None
|
except ConnectionClosed:
|
||||||
|
self._websocket = None
|
||||||
|
# 重连
|
||||||
|
print('掉线重连中')
|
||||||
|
await sleep(5)
|
||||||
|
continue
|
||||||
|
finally:
|
||||||
|
self._websocket = None
|
||||||
|
|
||||||
async def _send_auth(self):
|
async def _send_auth(self):
|
||||||
auth_params = {
|
auth_params = {
|
||||||
@ -96,15 +107,19 @@ class BLiveClient:
|
|||||||
await self._websocket.send(self._make_packet(auth_params, Operation.AUTH))
|
await self._websocket.send(self._make_packet(auth_params, Operation.AUTH))
|
||||||
|
|
||||||
async def _heartbeat_loop(self):
|
async def _heartbeat_loop(self):
|
||||||
try:
|
while True:
|
||||||
while True:
|
try:
|
||||||
if self._websocket is None:
|
if self._websocket is None:
|
||||||
await sleep(0.5)
|
await sleep(0.5)
|
||||||
else:
|
else:
|
||||||
await self._websocket.send(self._make_packet({}, Operation.SEND_HEARTBEAT))
|
await self._websocket.send(self._make_packet({}, Operation.SEND_HEARTBEAT))
|
||||||
await sleep(30)
|
await sleep(30)
|
||||||
except CancelledError:
|
|
||||||
pass
|
except CancelledError:
|
||||||
|
break
|
||||||
|
except ConnectionClosed:
|
||||||
|
# 等待重连
|
||||||
|
continue
|
||||||
|
|
||||||
async def _handle_message(self, message):
|
async def _handle_message(self, message):
|
||||||
offset = 0
|
offset = 0
|
||||||
@ -125,8 +140,12 @@ class BLiveClient:
|
|||||||
body = json.loads(body.decode('utf-8'))
|
body = json.loads(body.decode('utf-8'))
|
||||||
await self._handle_command(body)
|
await self._handle_command(body)
|
||||||
|
|
||||||
# elif header.operation == Operation.RECV_HEARTBEAT:
|
elif header.operation == Operation.RECV_HEARTBEAT:
|
||||||
# pass
|
pass
|
||||||
|
|
||||||
|
else:
|
||||||
|
body = message[offset + self.HEADER_STRUCT.size: offset + header.total_len]
|
||||||
|
print('未知包类型:', header, body)
|
||||||
|
|
||||||
offset += header.total_len
|
offset += header.total_len
|
||||||
|
|
||||||
@ -139,25 +158,28 @@ class BLiveClient:
|
|||||||
cmd = command['cmd']
|
cmd = command['cmd']
|
||||||
# print(command)
|
# print(command)
|
||||||
|
|
||||||
if cmd == 'DANMU_MSG': # 收到弹幕
|
if cmd == 'DANMU_MSG': # 收到弹幕
|
||||||
await self._on_get_danmaku(command['info'][1], command['info'][2][1])
|
await self._on_get_danmaku(command['info'][1], command['info'][2][1])
|
||||||
|
|
||||||
elif cmd == 'SEND_GIFT': # 送礼物
|
elif cmd == 'SEND_GIFT': # 送礼物
|
||||||
pass
|
pass
|
||||||
|
|
||||||
elif cmd == 'WELCOME': # 欢迎
|
elif cmd == 'WELCOME': # 欢迎
|
||||||
pass
|
pass
|
||||||
|
|
||||||
elif cmd == 'SYS_MSG': # 系统消息
|
elif cmd == 'WELCOME_GUARD': # 欢迎房管
|
||||||
pass
|
pass
|
||||||
|
|
||||||
elif cmd == 'PREPARING': # 房主准备中
|
elif cmd == 'SYS_MSG': # 系统消息
|
||||||
pass
|
pass
|
||||||
|
|
||||||
elif cmd == 'LIVE': # 直播开始
|
elif cmd == 'PREPARING': # 房主准备中
|
||||||
pass
|
pass
|
||||||
|
|
||||||
elif cmd == 'WISH_BOTTLE': # 许愿瓶?
|
elif cmd == 'LIVE': # 直播开始
|
||||||
|
pass
|
||||||
|
|
||||||
|
elif cmd == 'WISH_BOTTLE': # 许愿瓶?
|
||||||
pass
|
pass
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user