Пример #1
0
def process_recieve_data(sock, danmaku_queue, data):
    """Process recieving data.

    :param sock: the socket object.
    :param danmaku_queue: the queue to recieve danmaku.
    :param data: the recieved data.
    """
    data_type = convert_hexascii_to_int(data)
    if data_type == 1:
        hexascii_data = recieve_sock_data(sock, 4)
        if not hexascii_data:
            return False
        count = convert_hexascii_to_int(hexascii_data)
        if danmaku_queue.count != count:
            danmaku_queue.count = count
            print "当前直播人数为:{}".format(count)
    elif data_type == 4:
        hexascii_data = recieve_sock_data(sock, 2)
        if not hexascii_data:
            return False
        length = convert_hexascii_to_int(hexascii_data) - 4
        if length > 0:
            data = recieve_sock_data(sock, length)
            if not data:
                return False
            msg = ujson.loads(data)
            danmaku = generate_danmaku(msg)
            if danmaku:
                put_danmaku(danmaku_queue, danmaku)
                return True
Пример #2
0
 def get_danmaku(self):
     """ 请求获得弹幕信息。"""
     start = time.time()
     is_heath = True
     while True:
         end = time.time()
         if end - start > 10:
             start = time.time()
             try:
                 self.heartbeat()
             except socket.error:
                 is_heath = False
         try:
             data = self.socket.recv(2)
             if not data:
                 self.socket.close()
                 break
         except socket.timeout:
             if not is_heath:
                 print "连接超时,准备重连服务器。。。"
                 break
         except socket.error:
             break
         data_type = convert_hexascii_to_int(data)
         if data_type == 1:
             count = convert_hexascii_to_int(self.socket.recv(4))
             if self.danmaku_queue.set_count(count):
                 print "当前直播人数为:{0}".format(count)
         elif data_type == 4:
             length = convert_hexascii_to_int(self.socket.recv(2)) - 4
             if length > 0:
                 msg = self.socket.recv(length)
                 self.store_message(json.loads(msg))
Пример #3
0
def process_recieve_data(sock, danmaku_queue, data):
    """Process recieving data.

    :param sock: the socket object.
    :param danmaku_queue: the queue to recieve danmaku.
    :param data: the recieved data.
    """
    data_type = convert_hexascii_to_int(data)
    if data_type == 1:
        hexascii_data = recieve_sock_data(sock, 4)
        if not hexascii_data:
            return False
        count = convert_hexascii_to_int(hexascii_data)
        if danmaku_queue.count != count:
            danmaku_queue.count = count
            print "当前直播人数为:{}".format(count)
    elif data_type == 4:
        hexascii_data = recieve_sock_data(sock, 2)
        if not hexascii_data:
            return False
        length = convert_hexascii_to_int(hexascii_data) - 4
        if length > 0:
            data = recieve_sock_data(sock, length)
            if not data:
                return False
            msg = ujson.loads(data)
            danmaku = generate_danmaku(msg)
            if danmaku:
                put_danmaku(danmaku_queue, danmaku)
                return True