尝试使用websocket javascript clien连接到python套接字时出错

2024-04-20 08:47:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从浏览器中的javascript客户机连接到python服务器,并将数据从服务器发送到客户机,但是当尝试连接时,我收到错误:failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

我试着用wss代替ws。 我还试着用socket.io代替websocket,这也没用。我不记得我试过的所有事情,但都没用。在

Javascript客户端:

var conn = new WebSocket('ws://localhost:2001');
    conn.onmessage = function (event) {
     console.log(event.data);

Python服务器:

^{pr2}$

编辑:这是来自python的错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/joep/Documents/GitHub/Walnut-installation-web-app/testAPI/workers/sensorStream.py", line 26, in sensorStream
    sendSensorData.sendSensorData(data)
  File "/Users/joep/Documents/GitHub/Walnut-installation-web-app/testAPI/functions/sendSensorData.py", line 19, in sendSensorData
    conn.send(message)
BrokenPipeError: [Errno 32] Broken pipe

希望这是一个简单的技巧来解决它。在


Tags: inpyself服务器eventdata客户机ws