Sanic 类的add_websocket_route()方法的API接口。
add_websocket_route() 方法/函数
定义
add_websocket_route(handler, uri, host=None, strict_slashes=None, subprotocols=None, name=None)
用来把一个函数注册为WebSocket路由的方法。
参数
- handler :能够吃醋李websocket请求的函数或类实例。
- host:服务的host IP
- uri: 映射到WebSocket处理函数的URL路径。
- strict_slashes:严格匹配末尾的斜杠/ ,默认为False,即最后的/可有可无。
- subprotocols:用于WebSocket 握手的子协议。
- name:为url_for()方法定义的路由名称。
返回值
由websocket()装饰的对象。
例子
from sanic import Sanic
from sanic.response import file
app = Sanic(__name__)
@app.route('/')
async def index(request):
return await file('websocket.html')
async def feed(request, ws):
while True:
data = 'hello!'
print('Sending: ' + data)
await ws.send(data)
data = await ws.recv()
print('Received: ' + data)
app.add_websocket_route(feed)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)

我的公众号:猿人学 Python 上会分享更多心得体会,敬请关注。
***版权申明:若没有特殊说明,文章皆是猿人学 yuanrenxue.con 原创,没有猿人学授权,请勿以任何形式转载。***
说点什么吧...