使用0.0.0.0作为客户端html中的端点没有任何意义,并且您没有使用SSL,因此您想使用ws://而不是wss://。换一种说法,
from sanic import Sanicfrom sanic import responsefrom sanic.websocket import WebSocketProtocolapp = Sanic()@app.websocket('/feed')async def feed(request, ws): while True: data = 'hello!' print('Sending: ' + data) await ws.send(data) data = await ws.recv() print('Received: ' + data)@app.route('/html2')async def handle_request(request): return response.html("""<html><head><script> var exampleSocket = new WebSocket("ws://" + location.host + '/feed'); exampleSocket.onmessage = function (event) { console.log(event.data)};</script></head><body><h1>Hello socket!</h1><p>hello</p></body></html>""")app.run(host="0.0.0.0", port=8000)


