File: extensions.rst

package info (click to toggle)
python-websockets 15.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,948 kB
  • sloc: python: 25,105; javascript: 350; ansic: 148; makefile: 43
file content (39 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Write an extension
==================

.. currentmodule:: websockets

During the opening handshake, WebSocket clients and servers negotiate which
extensions_ will be used and with which parameters.

.. _extensions: https://datatracker.ietf.org/doc/html/rfc6455.html#section-9

Then, each frame is processed before being sent and after being received
according to the extensions that were negotiated.

Writing an extension requires implementing at least two classes, an extension
factory and an extension. They inherit from base classes provided by websockets.

Extension factory
-----------------

An extension factory negotiates parameters and instantiates the extension.

Clients and servers require separate extension factories with distinct APIs.
Base classes are :class:`~extensions.ClientExtensionFactory` and
:class:`~extensions.ServerExtensionFactory`.

Extension factories are the public API of an extension. Extensions are enabled
with the ``extensions`` parameter of :func:`~asyncio.client.connect` or
:func:`~asyncio.server.serve`.

Extension
---------

An extension decodes incoming frames and encodes outgoing frames.

If the extension is symmetrical, clients and servers can use the same class. The
base class is :class:`~extensions.Extension`.

Since extensions are initialized by extension factories, they don't need to be
part of the public API of an extension.