【问题标题】:How to fix websocket error: Error during WebSocket handshake: net::ERR_CONNECTION_RESET如何修复 websocket 错误:WebSocket 握手期间出错:net::ERR_CONNECTION_RESET
【发布时间】:2020-03-23 20:57:36
【问题描述】:

我想使用 Python 和 JavaScript 创建一个 Web 套接字,但出现以下错误。谢谢

到 'ws://localhost:1386/' 的 WebSocket 连接失败:连接建立错误:net::ERR_CONNECTION_REFUSED

Python 代码:

from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.bind(("localhost", 1386))
s.listen(5)
client, addr = s.accept()
print("connected to "+ str(addr))
client.sendall("hi. im server".encode())
client.close()

JavaScript - HTML 代码

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <script>
        var socket = new WebSocket('ws://localhost:1386');
    </script>
</head>
<body>

</body>
</html>

【问题讨论】:

  • 仅仅因为您在该端口上有一个套接字并不能使其成为 WebSocket。 WebSockets 在建立连接之前需要一系列特定于协议的握手。
  • 你好。我现在该怎么办?
  • 您可以查看 WebSocket 规范并实现正确的协议,或者您可以尝试查找已经实现该协议的 python 库。
  • 如果你使用 python > 3.6 有这个库websockets.readthedocs.io/en/stable/intro.html
  • 非常非常感谢

标签: javascript python websocket


【解决方案1】:

WebSocket 握手期间出错:net::ERR_CONNECTION_RESET

尝试将 'ws://localhost:1386' 更改为 'wss://localhost:1386'

ws >>> wss

【讨论】:

  • 协议名不是这里的问题。
猜你喜欢
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2019-07-01
相关资源
最近更新 更多