2018-08-28 13:58:03 +00:00
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
Starlette includes a `WebSocket` class that fulfils a similar role
|
|
|
|
to the HTTP request, but that allows sending and receiving data on a websocket.
|
2018-08-28 13:58:03 +00:00
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
### WebSocket
|
2018-08-28 13:58:03 +00:00
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
Signature: `WebSocket(scope, receive=None, send=None)`
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
```python
|
2018-09-05 09:29:04 +00:00
|
|
|
from starlette.websockets import WebSocket
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class App:
|
|
|
|
def __init__(self, scope):
|
|
|
|
self.scope = scope
|
|
|
|
|
|
|
|
async def __call__(self, receive, send):
|
2018-09-05 09:29:04 +00:00
|
|
|
websocket = WebSocket(self.scope, receive=receive, send=send)
|
|
|
|
await websocket.accept()
|
|
|
|
await websocket.send_text('Hello, world!')
|
|
|
|
await websocket.close()
|
2018-08-28 13:58:03 +00:00
|
|
|
```
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
WebSockets present a mapping interface, so you can use them in the same
|
2018-08-28 13:58:03 +00:00
|
|
|
way as a `scope`.
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
For instance: `websocket['path']` will return the ASGI path.
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
#### URL
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
The websocket URL is accessed as `websocket.url`.
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
The property is actually a subclass of `str`, and also exposes all the
|
|
|
|
components that can be parsed out of the URL.
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
For example: `websocket.url.path`, `websocket.url.port`, `websocket.url.scheme`.
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
#### Headers
|
|
|
|
|
|
|
|
Headers are exposed as an immutable, case-insensitive, multi-dict.
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
For example: `websocket.headers['sec-websocket-version']`
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
#### Query Parameters
|
|
|
|
|
|
|
|
Headers are exposed as an immutable multi-dict.
|
|
|
|
|
2018-10-29 09:22:45 +00:00
|
|
|
For example: `websocket.query_params['search']`
|
|
|
|
|
|
|
|
#### Path Parameters
|
|
|
|
|
|
|
|
Router path parameters are exposed as a dictionary interface.
|
|
|
|
|
|
|
|
For example: `request.path_params['username']`
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
### Accepting the connection
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
* `await websocket.accept(subprotocol=None)`
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
### Sending data
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
* `await websocket.send_text(data)`
|
|
|
|
* `await websocket.send_bytes(data)`
|
|
|
|
* `await websocket.send_json(data)`
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
### Receiving data
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
* `await websocket.receive_text()`
|
|
|
|
* `await websocket.receive_bytes()`
|
|
|
|
* `await websocket.receive_json()`
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
May raise `starlette.websockets.Disconnect()`.
|
|
|
|
|
|
|
|
### Closing the connection
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
* `await websocket.close(code=1000)`
|
2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
### Sending and receiving messages
|
|
|
|
|
|
|
|
If you need to send or receive raw ASGI messages then you should use
|
2018-09-05 09:29:04 +00:00
|
|
|
`websocket.send()` and `websocket.receive()` rather than using the raw `send` and
|
|
|
|
`receive` callables. This will ensure that the websocket's state is kept
|
2018-08-28 13:58:03 +00:00
|
|
|
correctly updated.
|
|
|
|
|
2018-09-05 09:29:04 +00:00
|
|
|
* `await websocket.send(message)`
|
|
|
|
* `await websocket.receive()`
|