Example #1
0
async def main():
    async def on_connect(ws):
        logging.info("connected.")
        await ws.send(WsApi.gen_join_room_pkg(MONITOR_ROOM_ID))

    async def on_shut_down():
        logging.error("shutdown!")
        raise RuntimeError("Connection broken!")

    async def on_message(message):
        for m in WsApi.parse_msg(message):
            try:
                await proc_message(m)
            except Exception as e:
                logging.error(f"Error happened when proc_message: {e}", exc_info=True)

    new_client = RCWebSocketClient(
        url=WsApi.BILI_WS_URI,
        on_message=on_message,
        on_connect=on_connect,
        on_shut_down=on_shut_down,
        heart_beat_pkg=WsApi.gen_heart_beat_pkg(),
        heart_beat_interval=10
    )

    await new_client.start()

    logging.info("DD ws stated.")
    while True:
        await asyncio.sleep(1)
Example #2
0
    def __init__(self):
        self._clients = {}
        self.monitor_live_rooms = {}

        self.msg_count = 0
        self._broken_live_rooms = []
        self.heartbeat_pkg = WsApi.gen_heart_beat_pkg()
Example #3
0
async def heart_beat():
    heart_beat_package = WsApi.gen_heart_beat_pkg()
    while True:
        await asyncio.sleep(30)
        for ws in ALL_WS_CLIENTS:
            if not ws.closed:
                await ws.send_bytes(heart_beat_package)
Example #4
0
    def __init__(self, room_id, on_message, on_broken):
        self.room_id = room_id
        self.on_message = on_message
        self.on_broken = on_broken

        self.session = None
        self.task = None
        self.task_heartbeat = None
        self._url = WsApi.BILI_WS_URI
        self._join_pkg = WsApi.gen_join_room_pkg(room_id=self.room_id)
        self._hb_pkg = WsApi.gen_heart_beat_pkg()

        self._connect_times = 0
        self._reconnect_time = 0
Example #5
0
async def test():
    from utils.biliapi import WsApi
    room_id = 2516117

    async def on_message(message):
        for msg in WsApi.parse_msg(message):
            print(f"on_message: {msg}")

    async def on_connect(ws):
        print(f"on_connect: {ws}")
        await ws.send(WsApi.gen_join_room_pkg(room_id))

    async def on_shut_down():
        print(f"on_shut_down, room_id: {room_id}")

    async def on_error(e, msg):
        print(f"on_error: e: {e}, msg: {msg}")

    new_client = RCWebSocketClient(
        url=WsApi.BILI_WS_URI,
        on_message=on_message,
        on_error=on_error,
        on_connect=on_connect,
        on_shut_down=on_shut_down,
        heart_beat_pkg=WsApi.gen_heart_beat_pkg(),
        heart_beat_interval=100
    )
    await new_client.start()

    count = 0
    while True:
        count += 1
        await asyncio.sleep(2)
        print(f"Status: {new_client.status}")

        if count == 20:
            print("Kill !")
            await new_client.kill()