WebSocket 连接已关闭:代码 = 1006

Posted

技术标签:

【中文标题】WebSocket 连接已关闭:代码 = 1006【英文标题】:WebSocket connection is closed: code = 1006 【发布时间】:2018-05-16 12:41:09 【问题描述】:

我正在连接到 websocket 客户端并订阅一些流。但我不断收到此错误

"'WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason'"

我不知道原因。谷歌搜索问题但没有帮助。 我正在使用 websockets 包。下面是我的代码。

async def streaming_data(url):
    headers = await gives_auth(auth_url)
    print(headers)
    try:
        async with websockets.client.connect(url,extra_headers=headers) as websocket:
            response = await websocket.recv()
            print(response)
    except websockets.exceptions.InvalidHandshake:
        print('Exception raised when a handshake request or response is invalid.')
    except websockets.exceptions.InvalidURI:
        print('Exception raised when an URI isn’t a valid websocket URI.')
    except websockets.exceptions.InvalidStatusCode:
        print('Handshake responce status code is invalid')
    except Exception as e:
        print('Error in making the Websocket Connection!!')
        print(e.args)



loop = asyncio.new_event_loop()
stream_url=""
streams = [loop.create_task(streaming_data(stream_url))]
loop.run_until_complete(asyncio.wait(streams))

我对此很陌生。感谢您的帮助。

【问题讨论】:

【参考方案1】:

1006 是保留值,不得将其设置为状态码 Close 控制框架 由端点。

它被指定用于 应用程序需要一个状态代码来表明 连接异常关闭,例如,没有发送或 接收Close 控制框架

【讨论】:

以上是关于WebSocket 连接已关闭:代码 = 1006的主要内容,如果未能解决你的问题,请参考以下文章