Windows上的Python 3.7
时from quart import Quart, websocket
app = Quart(__name__)
@app.route('/')
async def hello():
return 'hello'
@app.websocket('/ws')
async def ws():
while True:
await websocket.send('hello')
app.run()
Bad Request Bad request syntax or unsupported method
答案 0 :(得分:1)
您需要一个JS客户端来测试WebSocket,而不仅仅是浏览器,例如test-ws.html:
<!DOCTYPE html>
<html>
<body>
<script>
let socket = new WebSocket("ws://localhost:5000/ws");
socket.onmessage = function(event) {
alert(`Data received: ${event.data}`);
socket.close();
};
</script>
</body>
</html>
(使用python3 -m http.server并转到http://127.0.0.1:8000/test-ws.html在浏览器中进行测试)