我正在尝试在 MacOS X (11.2.3) 上使用 Visual Studio 代码在 python (3.9.0) 上设置 websocket。 遗憾的是,我没有运行连接。我立即收到“关闭连接”输出。 我已经尝试在 PyCharm 上运行代码......结果相同。我已经用谷歌搜索过了,但没有提供的解决方案对我有帮助。
这是我的代码,它绝对是基本的:
import websocket
SOCKET = "wss://echo.websocket.org"
def on_open(ws):
print('opened connection')
ws.send("hi")
def on_close(ws):
print('closed connection')
def on_message(ws, message):
print(message.upper())
ws.close()
ws = websocket.WebSocketApp(SOCKET, on_open=on_open,
on_close=on_close, on_message=on_message)
ws.run_forever()
非常感谢您的帮助。 最好的祝福。