Signals allow to set callbacks on some events:

  • ON_CONN_OPEN(socket, request) - when new websocket connection establishing, before authentication

  • ON_CONN_CLOSE(socket, request) - right before closing a connection

  • ON_CONN_FAIL(socket, request, err) - on connection upgrade failure

  • ON_AUTH_SUCCESS(socket, request) - after successfully authenticate new connection

  • ON_AUTH_FAIL(socket, request) - on authentication failure

  • ON_CALL_START(method, serial, args, kwargs) - on procedure call, before executing corresponding handler

  • ON_CALL_SUCCESS(method, serial, args, kwargs) - on success procedure call, before sending a reply

  • ON_CALL_FAIL(method, serial, args, kwargs) - on exception raised from procedure handler


import asyncio
import logging
import random
from contextvars import ContextVar
from time import time

import aiohttp.web
from wsrpc_aiohttp import (
    Route, STATIC_DIR, WebSocketAsync, WebSocketRoute, decorators

call_started_at = ContextVar("call_started_at")

log = logging.getLogger(__name__)

class TestRoute(Route):

    async def slow_proc(self):
        await asyncio.sleep(random.randint(1, 10) / 10)
        return True

# Signal handlers
async def on_call_start(method, **kwargs):
    ts = time()
    log.debug("Method %s called at %f", method, ts)

async def on_call_end(method, **kwargs):
    ts = time() - call_started_at.get()"Method %s processed for %f", method, ts)

# Connecting handlers to signals

app = aiohttp.web.Application()
app.router.add_route("*", "/ws/", WebSocketAsync)  # Websocket route
app.router.add_static('/', ".")  # Your static files

WebSocketAsync.add_route('test', TestRoute)

if __name__ == '__main__':
    aiohttp.web.run_app(app, port=8000, access_log=None)