Merge pull request #268 from taoufik07/patch-18

Websocket docs
This commit is contained in:
2018-12-29 07:04:45 -05:00
committed by GitHub
2 changed files with 29 additions and 0 deletions
+1
View File
@@ -47,6 +47,7 @@ Features
- A pleasant API, with a single import statement.
- Class-based views without inheritance.
- ASGI framework, the future of Python web services.
- WebSocket support!
- The ability to mount any ASGI / WSGI app at a subroute.
- *f-string syntax* route declaration.
- Mutable response object, passed into each view. No need to return anything.
+28
View File
@@ -184,6 +184,34 @@ If you'd like a view to be executed before every request, simply do the followin
Now all requests to your HTTP Service will include an ``X-Pizza`` header.
WebSocket Support
-----------------
Responder supports WebSockets::
@api.route('/ws', websocket=True)
async def websocket(ws):
await ws.accept()
while True:
name = await ws.receive_text()
await ws.send_text(f"Hello {name}!")
await ws.close()
Accepting the connection::
await websocket.accept()
Sending and receiving data::
await websocket.send_{format}(data)
await websocket.receive_{format}(data)
Supported formats: ``text``, ``json``, ``bytes``.
Closing the connection::
await websocket.close()
Using Requests Test Client
--------------------------