gpt4 book ai didi

javascript - Python/JavaScript - WebSockets 连接问题

转载 作者:行者123 更新时间:2023-12-01 17:16:01 24 4
gpt4 key购买 nike

我有几个错误 WebSockets 服务器。
以前,我在建立连接时确认服务器时遇到了很多问题。在绑定(bind)地址中,我有一个等于 0.0.0.0 的空字符串所以我认为问题可能来自那里。目前,客户端只有在服务器停止 time 时才能知道它已连接。异步函数。我有时会收到错误send.html:14 WebSocket connection to ws://86.205.245.32:5678/ failed: WebSocket opening handshake timed out但很少。
我有很多问题可以回答这个帖子。

  • 可以使用子协议(protocol)帮助吗?如果是,如何将它与 Python 服务器一起使用?
  • 可以使用 WSS 帮助吗?
  • 是否需要更改0.0.0.0的绑定(bind)IP?给另一个?

  • 服务器
    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
    我想使用 非军事区 因为我的网络很奇怪,有很多保护,所以我认为把服务器放在 非军事区 会有所帮助。现在,我不能告诉你它是否有帮助,因为现在当客户端执行 1* see on the client code 行时它无法连接到服务器。奇怪的是没有 非军事区 和windows电脑上的服务器一样,只是为了发送数据,客户端只有在服务器关闭连接时才能看到服务器的数据。
    我尝试了什么
  • 我试图停用防火墙
  • 我在 StackOverflow
  • 上提出了问题

    当我在客户端执行期间关闭服务器时,我经常遇到这个问题,即使它没有执行 block 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 计算机(客户端)
  • 树莓派(服务器)

  • 感谢您的帮助,让我知道您是否已经遇到此问题以及您采取了哪些措施来解决它。或者,如果您在代码中发现了可能导致此类问题的错误
    谢谢

    最佳答案

    我试图重现您的问题,但我认为您的问题是由您的网络基础设施引起的。
    服务器 客户是非常基本的,他们工作顺利。
    将您的问题分为不同的阶段:

  • 使用本地环境调试代码( 客户端 服务器 localhost 中,在您的 PC 上)
  • 使用您的本地网络在 之间进行第一次隔离。客户服务器
  • 使用 Cloud Providers 上的一些免费计划来测试扩展网络(AWS、GCP、DigitalOcean)

  • 一些建议可能是:
  • 完全禁用 Windows 防火墙
  • 检查您的 Linux 发行版(UFW、IPTables...)上是否没有防火墙
  • 使用 Raspberry Pi 的 LAN IP 而非其公共(public) IP
  • 进行测试
  • 确保您的 Livebox Play 中没有防火墙或某些路由规则
  • 关于javascript - Python/JavaScript - WebSockets 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62818066/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com