Пример #1
0
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    global is_first
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    userid = int(100000000 * random.random())
    body = ('{"roomid": ' + str(room_id) + ', "uid": ' + str(userid) + '}')
    while True:
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = get_server(room_id)
            sock.connect((address, 788))
            send_data = send_socket_data(sock, 16 + len(body), 16, 1, 7, 1,
                                         body)
        except socket.error as exc:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()
Пример #2
0
def heartbeat(sock, danmaku_queue, is_health):
    """Keep the connection alive.

    :param sock: the socket object.
    :param danmaku_queue: the queue to recieve danmaku.
    :param is_health: the status of connection
    """
    
    while True:
        try:
            send_data = send_socket_data(sock, 16, 16, 1, 2)
        except socket.timeout:
            is_health = False
        produce_danmaku.switch(sock, danmaku_queue, is_health)
Пример #3
0
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    global is_first
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    userid = int(100000000 * random.random())
    body = ('{"roomid": ' + str(room_id) + ', "uid": ' + str(userid) +'}')
    while True:
        try:
            sock = socket.socket(socket.AF_INET,
                             socket.SOCK_STREAM)
            address = get_server(room_id)
            sock.connect(
                (address, 788))
            send_data = send_socket_data(sock, 16 + len(body), 16,
                    1, 7, 1, body)
        except socket.error as exc:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()