Sanic如何用http和ws响应?


问题内容

我在基于结合不同端点的Sanic hello世界中具有以下代码:

代码是:

from sanic import Sanic
from sanic import response
from sanic.websocket import WebSocketProtocol

app = Sanic()

@app.route("/")
async def test(request):
    return response.json({"hello": "world"})


@app.route('/html')
async def handle_request(request):
    return response.html('<p>Hello world!</p>')

@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("wss://0.0.0.0:8000/feed",      "protocolOne");
         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)
# app.run(host="0.0.0.0", port=8000, protocol=WebSocketProtocol)    # ws

路由“ /”和“ / html”可以正常工作,但是

http://0.0.0.0:8000/feed

产生:

Error: Invalid websocket request

和“ / html2”可以使页面正常显示,但不会登录到控制台,在调试器中显示:

Firefox can’t establish a connection to the server at wss://0.0.0.0:8000/feed.

我应该更改或添加什么以使可行的websocket端点也能很好地与http一起玩?


问题答案:

使用0.0.0.0作为客户端html中的端点没有任何意义,并且您没有使用SSL,因此您想使用ws://而不是wss://。换一种说法,

from sanic import Sanic
from sanic import response
from sanic.websocket import WebSocketProtocol

app = 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)