本文整理汇总了Python中sanic.Sanic.add_websocket_route方法的典型用法代码示例。如果您正苦于以下问题:Python Sanic.add_websocket_route方法的具体用法?Python Sanic.add_websocket_route怎么用?Python Sanic.add_websocket_route使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sanic.Sanic
的用法示例。
在下文中一共展示了Sanic.add_websocket_route方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_websocket_route [as 别名]
def __init__(self, daemon):
app = Sanic('ansible-api')
app.add_route(controller.Main.as_view(), '/')
app.add_route(controller.NonBlockTest.as_view(), '/test')
app.add_route(controller.Command.as_view(), '/command')
app.add_route(controller.Playbook.as_view(), '/playbook')
app.add_route(controller.FileList.as_view(), '/filelist')
app.add_route(controller.FileReadWrite.as_view(), '/fileitem')
app.add_route(controller.FileExist.as_view(), '/filexist')
app.add_route(controller.ParseVarsFromFile.as_view(), '/parsevars')
app.add_websocket_route(controller.Message.websocket, '/message', subprotocols=Config.get('ws_sub'))
app.config.update(dict(RESPONSE_TIMEOUT=Config.get('timeout'))) # timeout for waiting response
@app.middleware('request')
async def ip_ban(request):
if len(Config.get('allow_ip')) and request.ip not in Config.get('allow_ip'):
return text('Your IP (%s) is not allowed!' % request.ip, status=403)
# print config contents
config = Config().__dict__
config['sign_key'] = len(config['sign_key']) * '*' # mask signature key
Tool.LOGGER.debug("Config at start: %s" % json.dumps(config))
app.run(host=Config.get('host'), port=Config.get('port'), workers=Config.get('workers'), debug=not daemon)
示例2: echo
# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_websocket_route [as 别名]
from sanic import Sanic
async def echo(request, ws):
while True:
data = await ws.recv()
print(data)
if data == "close":
await ws.close()
else:
await ws.send(data + "/answer")
app = Sanic(__name__)
app.add_websocket_route(echo, "/ws")
app.run(host="0.0.0.0", port=8080, debug=True)
示例3:
# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_websocket_route [as 别名]
# if consumer:
# consumer.stop()
should_quite = True
if should_quite:
return
await asyncio.sleep(SOCKET_SLEEP)
EXPERIMENT_URL = '/v1/<username>/<project_name>/experiments/<experiment_sequence>'
WS_EXPERIMENT_URL = '/ws{}'.format(EXPERIMENT_URL)
# Job urls
app.add_websocket_route(
job_resources,
'{}/jobs/<job_sequence>/resources'.format(EXPERIMENT_URL))
app.add_websocket_route(
job_resources,
'{}/jobs/<job_sequence>/resources'.format(WS_EXPERIMENT_URL))
app.add_websocket_route(
job_logs,
'{}/jobs/<job_sequence>/logs'.format(EXPERIMENT_URL))
app.add_websocket_route(
job_logs,
'{}/jobs/<job_sequence>/logs'.format(WS_EXPERIMENT_URL))
# Experiment urls
app.add_websocket_route(