【问题标题】:Websocket client not receiving dataWebsocket客户端未接收数据
【发布时间】:2020-02-05 18:36:16
【问题描述】:

我有一个奇怪的问题。在阅读了 websocket-client 之后,似乎很容易,通过websocket.send() 发送数据并通过recv() 接收数据。

我已经设置了我的 websocket 来读取和发送一个二进制文件。代码如下

ws = websocket.WebSocket()
ws.connect(uri)

text_list = list()

# setup the receiving portion
receiving_tread = threading.Thread(target=thread_receiving, args=(ws, text_list))
receiving_tread.start()

ws.send_binary([48])  # telling server file has started

f = open(binary_file, 'rb')

chunk = f.read(8000)

while chunk != b'':
    ws.send(chunk)

    time.sleep(0.5)
    chunk = f.read(8000)

ws.send_binary([49]) # tell server file has ended

接收数据的过程:

def thread_receiving(ws, text_list):
    data = json.loads(ws.recv())
    text_list.append(data)

我知道数据已发送,因为我收到了返回数据的第一部分,然后它停止了。所以我只得到 1 个回报,不知何故 recv() 停止收听。注意:我无法访问服务器或服务器 websocket,所以我必须从客户端进行故障排除。

谁能告诉我,我做错了什么?

【问题讨论】:

    标签: python-3.x websocket binary


    【解决方案1】:

    好的,我找到了答案,看来接收函数运行后就退出了。所以我们需要让它继续运行。

    因此,

    def thread_receiving(ws, text_list):
        data = json.loads(ws.recv())
    
        while len(data) > 0
            text_list.append(data)
            data = json.loads(ws.recv())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      相关资源
      最近更新 更多