You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/websockets/index.md
+14-11Lines changed: 14 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -185,12 +185,13 @@ The `ws_route` offers more than just defining a websocket route. It can also be
185
185
By setting `use_extra_handler=True` in `ws_route` decorator, we activate an in-built handler that gives the ability to
186
186
manage different sessions of websocket differently like `on_connect`, `on_message` and `on_disconnect`
187
187
188
-
-`on_connect(websocket, **kwargs)`: handler to handles client connection with the server.
189
-
-`on_message(websocket, data)`: handler for messages received from the client
190
-
-`on_disconnect(websocket, close_code)`: handler that handles client disconnecting from websocket server
188
+
-`on_connect(websocket, **kwargs)`: handles client connection with the server.
189
+
-`on_message(websocket, data)`: handles messages sent from the client
190
+
-`on_disconnect(websocket, close_code)`: handles server disconnecting from client
191
191
192
-
This approach also enables message data type validation using `WsBody`.
193
-
`WsBody` is similar to [`Body`](../parsing-inputs/body) but for websockets.
192
+
!!! info
193
+
This approach also enables message data type validation using `WsBody`.
194
+
`WsBody` is similar to [`Body`](../parsing-inputs/body) but for websockets.
194
195
195
196
Let's rewrite the previous example, `/live-support` websocket route.
196
197
```python
@@ -224,22 +225,24 @@ class CarController(ControllerBase):
224
225
await websocket.close(code)
225
226
```
226
227
In the construct above, we created `def live_support_connect` to handle connection to the `'/live-support'` websocket route and
227
-
`def live_support_disconnect` to handle disconnection from it. `def live_support_connect` and `def live_support_disconnect` takes `websocket` instance as only parameter and must be an asynchronous function.
228
+
`def live_support_disconnect` to handle disconnection from it. `def live_support_connect` and `def live_support_disconnect`
229
+
takes `websocket` instance as only parameter and must be an **asynchronous** function.
228
230
229
-
On the other hand,`def live_support` function is now a **message receiver handler** and so, there is need to define a parameter with `WsBody`, in this case `data:str = WsBody()`. And message sent from client will be passed to `data` parameter and validated as `str` data type.
231
+
On the other hand,`def live_support` function is now a **message receiver handler** and so, there is need to define a parameter with `WsBody`,
232
+
in this case `data:str = WsBody()`. Message sent from client will be passed to `data` parameter after validation and procession by `WsBody`.
230
233
If validation fails, an error will be sent to the client and connection will be destroyed.
231
234
232
-
The **`encoding` = 'text'** states the **message** data structure expected from client to the server.
233
-
There are 3 different **`encoding`** types.
235
+
The **`encoding` = 'text'** states the **message** data structure that is required of the client when sending messages to the server.
236
+
There are other **`encoding`** types supported:
234
237
235
238
-`text`: allows only simple text messages as in the case above, e.g. `@ws_route('/path', use_extra_handler=True, encoding='text')`
236
239
-`json`: allows json messages e.g. `@ws_route('/path', use_extra_handler=True, encoding='json')`
237
240
-`bytes`: allows byte messages e.g. `@ws_route('/path', use_extra_handler=True, encoding='bytes')`
238
241
239
242
**Simplifying the example above**
240
243
241
-
We can further simplify the example above by getting rid of the `live_support_connect` and `live_support_disconnect`
242
-
and let the inbuilt handler manage that for us as shown below:
244
+
We can further simplify the example above by getting rid of the `live_support_connect` and `live_support_disconnect`
245
+
and let the inbuilt handler apply the default `connection` and `disconnection` actions.
0 commit comments