websocket服务器响应头,WebSocket:WebSocket握手期间出错:发送了非空的“Sec WebSocket Protocol”头,但未收到响应...

The WebSocket(url, protocols) constructor takes one or two arguments. The first argument, url, specifies the URL to which to connect. The second, protocols, if present, is either a string or an array of strings. If it is a string, it is equivalent to an array consisting of just that string; if it is omitted, it is equivalent to the empty array. Each string in the array is a subprotocol name. The connection will only be established if the server reports that it has selected one of these subprotocols. The subprotocol names must all be strings that match the requirements for elements that comprise the value of Sec-WebSocket-Protocol fields as defined by the WebSocket protocol specification.

您的服务器用空的Sec-WebSocket-Protocol头响应websocket连接请求,因为它不支持Chat-1子目录。

因为您同时在编写服务器端和客户端(并且除非您编写了要共享的API),所以设置特定的子目录名称不应该是非常重要的。

可以通过从javascript连接中删除子目录名称来解决此问题:var socket = new WebSocket(serviceUrl);

或者修改服务器以支持请求的协

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`AttributeError: __aenter__` 错误通常表示您在 `websockets.connect()` 方法上使用了 `async with` 语句,但该方法不支持上下文管理器(context manager)。 正常情况下,您应该使用 `async with` 语句来确保在使用完 WebSocket 之后正确关闭连接。但是,`websockets.connect()` 并不是一个上下文管理器,因此会抛出该错误。 要解决这个问题,您可以使用 `websockets.client.connect()` 方法来建立 WebSocket 连接,然后手动管理连接的打开和关闭。 以下是一个示例代码,展示了如何使用 `websockets.client.connect()` 来建立 WebSocket 连接: ```python import asyncio import websockets async def client(): # 建立 WebSocket 连接 websocket = await websockets.client.connect('ws://websocket-server-url') try: # 发送和接收消息的代码在这里实现 await websocket.send("Hello, server!") response = await websocket.recv() print(f"Received response from server: {response}") finally: # 关闭 WebSocket 连接 await websocket.close() # 启动 WebSocket 客户端 asyncio.get_event_loop().run_until_complete(client()) ``` 在上述代码中,我们使用 `websockets.client.connect()` 方法建立 WebSocket 连接,并在 `try-finally` 语句中发送和接收消息。无论是否出现异常,`finally` 块中的代码都会执行,确保 WebSocket 连接被正确关闭。 请注意,上述代码只是一个示例,您需要将 `'ws://websocket-server-url'` 替换为实际的 WebSocket 服务器 URL。 希望这可以解决您的问题!如果您还有其他疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值