我在使用WebSockets服务器时遇到了几个错误。
以前,我在建立连接时确认服务器时遇到了很多问题。在绑定地址中,我有一个等于的空字符串,0.0.0.0
所以我认为问题可能来自那里。time
目前,客户端只有在服务器停止异步功能时才能知道它已经连接。我有时会收到错误
send.html:14 WebSocket connection to ws://86.205.245.32:5678/ failed: WebSocket opening handshake timed out
但很少。
我有很多问题可以回答这个帖子。
可以使用子协议帮助吗?如果是,如何将它与 Python 服务器一起使用?
可以使用 WSS 帮助吗?
我需要将绑定IP更改
0.0.0.0
为另一个吗?
服务器
print("start serveur")
import time, websockets, asyncio
async def time(websocket, path):
print("connect")
print(websocket)
print(await websocket.recv())
start_server = websockets.serve(time, "", 5678)
loop=asyncio.get_event_loop()
loop.run_until_complete(start_server)
loop.run_forever()
服务器的回溯
Error in connection handler
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 674, in transfer_data
message = yield from self.read_message()
File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 742, in read_message
frame = yield from self.read_data_frame(max_size=self.max_size)
File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 815, in read_data_frame
frame = yield from self.read_frame(max_size)
File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 884, in read_frame
extensions=self.extensions,
File "/usr/local/lib/python3.5/dist-packages/websockets/framing.py", line 99, in read
data = yield from reader(2)
File "/usr/lib/python3.5/asyncio/streams.py", line 668, in readexactly
yield from self._wait_for_data('readexactly')
File "/usr/lib/python3.5/asyncio/streams.py", line 458, in _wait_for_data
yield from self._waiter
File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 285, in result
raise CancelledError
concurrent.futures._base.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/websockets/server.py", line 169, in handler
yield from self.ws_handler(self, path)
File "webserveur.py", line 8, in time
print(await websocket.recv())
File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 434, in recv
yield from self.ensure_open()
File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 658, in ensure_open
) from self.transfer_data_exc
websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason
客户端
<script>
alert("hello");
var ws = new WebSocket("ws://86.205.245.32:5678/" );//1*
ws.onopen = function (event) {
alert("had a handshake answer that cause a connection");
ws.send("hello");
}
</script>
WebSockets 请求
General
Request URL: ws://86.205.245.32:5678/
Request Headers
Provisional headers are shown
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,fr;q=0.7
Cache-Control: no-cache
Connection: Upgrade
Host: 86.205.245.32:5678
Origin: file://
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: M+CfTACesBfbhkWnFclrsA==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
我想使用DMZ,因为我的网络很奇怪,有很多保护,所以我认为将服务器置于非军事区会有所帮助。目前,我无法告诉您它是否有帮助,因为目前当客户端执行该行时1* see on the client code
,它无法连接到服务器。奇怪的是,没有DMZ和Windows电脑上的服务器,都是一样的,只是为了发送数据,客户端只有在服务器关闭连接时才能看到服务器的数据。
我尝试了什么
- 我试图停用防火墙
- 我在 StackOverflow 上提问
当我在客户端执行期间关闭服务器时,我经常遇到这个问题,即使它没有执行time
打印的块print("connect")
:
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketServerProtocol.handler() running at /usr/local/lib/python3.5/dist-packages/websockets/server.py:117> wait_for=<Future pending cb=[Task._wakeup()]>>
我还按如下方式更改了客户端的代码,但没有成功。
var ws = new WebSocket("ws://86.205.245.32:5678/"),
messages = document.createElement('ul');
ws.onopen = function (event) {
alert("connect");
ws.send("hello");
}
硬件
一台 Windows 计算机(客户端)
树莓派(服务器)
感谢您的帮助,让我知道您是否已经遇到此问题以及您采取了哪些措施来解决它。或者,如果您在代码中发现可能导致此类问题的错误
谢谢