Skip to content

Commit b0fb5a2

Browse files
author
IURII Shikanov
committed
Add signals to docs
1 parent 02ffada commit b0fb5a2

File tree

3 files changed

+74
-72
lines changed

3 files changed

+74
-72
lines changed

README.md

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -144,78 +144,6 @@ Frontend code:
144144
</script>
145145
```
146146

147-
## Signals
148-
149-
Signals allow to set callbacks on some events:
150-
151-
- `ON_CONN_OPEN(socket, request)` - when new websocket connection establishing,
152-
before authentication
153-
- `ON_CONN_CLOSE(socket, request)` - right before closing a connection
154-
- `ON_AUTH_SUCCESS(socket, request)` - after successfully authenticate
155-
new connection
156-
- `ON_AUTH_FAIL(socket, request)` - on authentication failure
157-
- `ON_CALL_START(method, serial, args, kwargs)` - on procedure call,
158-
before executing corresponding handler
159-
- `ON_CALL_SUCCESS(method, serial, args, kwargs)` - on success procedure call,
160-
before sending a reply
161-
- `ON_CALL_FAIL(method, serial, args, kwargs)` - on exception raised
162-
from procedure handler
163-
164-
Example:
165-
166-
```python
167-
import asyncio
168-
import logging
169-
import random
170-
from contextvars import ContextVar
171-
from time import time
172-
173-
import aiohttp.web
174-
from wsrpc_aiohttp import (
175-
Route, STATIC_DIR, WebSocketAsync, WebSocketRoute, decorators
176-
)
177-
178-
179-
call_started_at = ContextVar("call_started_at")
180-
181-
log = logging.getLogger(__name__)
182-
183-
184-
class TestRoute(Route):
185-
186-
@decorators.proxy
187-
async def slow_proc(self):
188-
await asyncio.sleep(random.randint(1, 10) / 10)
189-
return True
190-
191-
# Signal handlers
192-
async def on_call_start(method, **kwargs):
193-
ts = time()
194-
log.debug("Method %s called at %f", method, ts)
195-
call_started_at.set(ts)
196-
197-
198-
async def on_call_end(method, **kwargs):
199-
ts = time() - call_started_at.get()
200-
log.info("Method %s processed for %f", method, ts)
201-
202-
# Connecting handlers to signals
203-
WebSocketAsync.ON_CALL_START.connect(on_call_start)
204-
WebSocketAsync.ON_CALL_SUCCESS.connect(on_call_end)
205-
206-
207-
app = aiohttp.web.Application()
208-
app.router.add_route("*", "/ws/", WebSocketAsync) # Websocket route
209-
app.router.add_static('/', ".") # Your static files
210-
211-
WebSocketAsync.add_route('test', TestRoute)
212-
213-
214-
if __name__ == '__main__':
215-
logging.basicConfig(level=logging.INFO)
216-
aiohttp.web.run_app(app, port=8000, access_log=None)
217-
```
218-
219147
## Versioning
220148

221149
This software follows [Semantic Versioning](http://semver.org/)

docs/source/02-signals.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Signals
2+
=======
3+
4+
Signals allow to set callbacks on some events:
5+
6+
- ``ON_CONN_OPEN(socket, request)`` - when new websocket connection
7+
establishing, before authentication
8+
- ``ON_CONN_CLOSE(socket, request)`` - right before closing a
9+
connection
10+
- ``ON_AUTH_SUCCESS(socket, request)`` - after successfully
11+
authenticate new connection
12+
- ``ON_AUTH_FAIL(socket, request)`` - on authentication failure
13+
- ``ON_CALL_START(method, serial, args, kwargs)`` - on procedure call,
14+
before executing corresponding handler
15+
- ``ON_CALL_SUCCESS(method, serial, args, kwargs)`` - on success
16+
procedure call, before sending a reply
17+
- ``ON_CALL_FAIL(method, serial, args, kwargs)`` - on exception raised
18+
from procedure handler
19+
20+
Example:
21+
22+
.. code:: python
23+
24+
import asyncio
25+
import logging
26+
import random
27+
from contextvars import ContextVar
28+
from time import time
29+
30+
import aiohttp.web
31+
from wsrpc_aiohttp import (
32+
Route, STATIC_DIR, WebSocketAsync, WebSocketRoute, decorators
33+
)
34+
35+
36+
call_started_at = ContextVar("call_started_at")
37+
38+
log = logging.getLogger(__name__)
39+
40+
41+
class TestRoute(Route):
42+
43+
@decorators.proxy
44+
async def slow_proc(self):
45+
await asyncio.sleep(random.randint(1, 10) / 10)
46+
return True
47+
48+
# Signal handlers
49+
async def on_call_start(method, **kwargs):
50+
ts = time()
51+
log.debug("Method %s called at %f", method, ts)
52+
call_started_at.set(ts)
53+
54+
55+
async def on_call_end(method, **kwargs):
56+
ts = time() - call_started_at.get()
57+
log.info("Method %s processed for %f", method, ts)
58+
59+
# Connecting handlers to signals
60+
WebSocketAsync.ON_CALL_START.connect(on_call_start)
61+
WebSocketAsync.ON_CALL_SUCCESS.connect(on_call_end)
62+
63+
64+
app = aiohttp.web.Application()
65+
app.router.add_route("*", "/ws/", WebSocketAsync) # Websocket route
66+
app.router.add_static('/', ".") # Your static files
67+
68+
WebSocketAsync.add_route('test', TestRoute)
69+
70+
71+
if __name__ == '__main__':
72+
logging.basicConfig(level=logging.INFO)
73+
aiohttp.web.run_app(app, port=8000, access_log=None)

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Features
5858
* Protected server-side methods (starts with underline never will be call
5959
from clients-side directly)
6060
* If `ujson`_ is installed messages will be serialize/deserialize with it.
61+
* Introspection with signals
6162

6263

6364
Installation

0 commit comments

Comments
 (0)