对于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.0.0作为客户端html中的端点没有任何意义,并且您没有使用SSL,因此您想使用ws://而不是wss://。换句话说,
<a href="#" class="item">item 1</a>
<a href="#" class="item">item 2</a>
<a href="#" class="item">item 3</a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>