Debian Codegraph @

docs/intro/examples.rst

raw
1 Quick examples
2 ==============
3
4 .. currentmodule:: websockets
5
6 Start a server
7 --------------
8
9 This WebSocket server receives a name from the client, sends a greeting, and
10 closes the connection.
11
12 .. literalinclude:: ../../example/quick/server.py
13 :caption: server.py
14 :language: python
15
16 :func:`~asyncio.server.serve` executes the connection handler coroutine
17 ``hello()`` once for each WebSocket connection. It closes the WebSocket
18 connection when the handler returns.
19
20 Connect a client
21 ----------------
22
23 This WebSocket client sends a name to the server, receives a greeting, and
24 closes the connection.
25
26 .. literalinclude:: ../../example/quick/client.py
27 :caption: client.py
28 :language: python
29
30 Using :func:`~sync.client.connect` as a context manager ensures that the
31 WebSocket connection is closed.
32
33 Connect a browser
34 -----------------
35
36 The WebSocket protocol was invented for the web — as the name says!
37
38 Here's how to connect a browser to a WebSocket server.
39
40 Run this script in a console:
41
42 .. literalinclude:: ../../example/quick/show_time.py
43 :caption: show_time.py
44 :language: python
45
46 Save this file as ``show_time.html``:
47
48 .. literalinclude:: ../../example/quick/show_time.html
49 :caption: show_time.html
50 :language: html
51
52 Save this file as ``show_time.js``:
53
54 .. literalinclude:: ../../example/quick/show_time.js
55 :caption: show_time.js
56 :language: js
57
58 Then, open ``show_time.html`` in several browsers or tabs. Clocks tick
59 irregularly.
60
61 Broadcast messages
62 ------------------
63
64 Let's send the same timestamps to everyone instead of generating independent
65 sequences for each connection.
66
67 Stop the previous script if it's still running and run this script in a console:
68
69 .. literalinclude:: ../../example/quick/sync_time.py
70 :caption: sync_time.py
71 :language: python
72
73 Refresh ``show_time.html`` in all browsers or tabs. Clocks tick in sync.
74
75 Manage application state
76 ------------------------
77
78 A WebSocket server can receive events from clients, process them to update the
79 application state, and broadcast the updated state to all connected clients.
80
81 Here's an example where any client can increment or decrement a counter. The
82 concurrency model of :mod:`asyncio` guarantees that updates are serialized.
83
84 This example keep tracks of connected users explicitly in ``USERS`` instead of
85 relying on :attr:`server.connections <asyncio.server.Server.connections>`. The
86 result is the same.
87
88 Run this script in a console:
89
90 .. literalinclude:: ../../example/quick/counter.py
91 :caption: counter.py
92 :language: python
93
94 Save this file as ``counter.html``:
95
96 .. literalinclude:: ../../example/quick/counter.html
97 :caption: counter.html
98 :language: html
99
100 Save this file as ``counter.css``:
101
102 .. literalinclude:: ../../example/quick/counter.css
103 :caption: counter.css
104 :language: css
105
106 Save this file as ``counter.js``:
107
108 .. literalinclude:: ../../example/quick/counter.js
109 :caption: counter.js
110 :language: js
111
112 Then open ``counter.html`` file in several browsers and play with [+] and [-].