Пример #1
0
def _setup_single_view_dispatcher_route(
        blueprint: Blueprint, constructor: RootComponentConstructor) -> None:
    async def model_stream(request: request.Request,
                           socket: WebSocketCommonProtocol,
                           path: str = "") -> None:
        send, recv = _make_send_recv_callbacks(socket)
        conn = Connection(request, socket, path)
        await serve_json_patch(
            Layout(ConnectionContext(constructor(), value=conn)),
            send,
            recv,
        )

    blueprint.add_websocket_route(model_stream, "/_api/stream")
    blueprint.add_websocket_route(model_stream, "/<path:path>/_api/stream")
Пример #2
0
        isPresenter = True

    # 连接成功回调
    await ws_listener.onConnected(ws, roomId, uid, isPresenter)
    # 持续监听
    while True:
        try:
            # 等待收到消息
            data = await ws.recv()
            import json

            log.info('ws receive message:{}'.format(data))

            try:
                # 消息解包成GamePacket对象
                packet = json.loads(data, object_hook=GamePacket.load)
                # 回调业务访问
                await ws_listener.onData(ws, packet)
            except Exception as e:
                log.error('ws deal message error:{}'.format(str(e)))

        except Exception as e:
            log.error('ws receive message error:{}'.format(str(e)))
            # 回调连接断开方法
            await ws_listener.onDisconnected(ws)
            break


# 注册websocket处理方法
ws_bp.add_websocket_route(ws_bp_root, '/')
Пример #3
0
from sanic import Blueprint

from .views import increment

bp = Blueprint(__name__.split('.')[0], url_prefix='/')
bp.add_websocket_route(handler=increment, uri="increment")
Пример #4
0
from sanic import Blueprint

from .websockets import chat

bp = Blueprint(__name__.split('.')[0], url_prefix='/')

bp.add_websocket_route(handler=chat, uri="chat")