如何让Sanic响应http和ws?
How to have Sanic respond with http and ws?
我有以下基于此处组合不同端点的 Sanic hello world 代码:
- https://sanic.readthedocs.io/en/latest/sanic/response.html
- https://sanic.readthedocs.io/en/latest/sanic/websocket.html
代码是:
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 端点配合使用?
在客户端 html 中使用 0.0.0.0 作为端点没有任何意义,而且您没有使用 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)
我有以下基于此处组合不同端点的 Sanic hello world 代码:
- https://sanic.readthedocs.io/en/latest/sanic/response.html
- https://sanic.readthedocs.io/en/latest/sanic/websocket.html
代码是:
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 端点配合使用?
在客户端 html 中使用 0.0.0.0 作为端点没有任何意义,而且您没有使用 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)