Skip to content

Commit 87a7302

Browse files
committed
Add example of client shutdown.
Fix #933.
1 parent 2080026 commit 87a7302

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

docs/api/index.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
API
2+
===
3+
4+
``websockets`` provides complete client and server implementations, as shown
5+
in the :doc:`getting started guide <intro>`.
6+
7+
The process for opening and closing a WebSocket connection depends on which
8+
side you're implementing.
9+
10+
* On the client side, connecting to a server with :class:`~websockets.connect`
11+
yields a connection object that provides methods for interacting with the
12+
connection. Your code can open a connection, then send or receive messages.
13+
14+
If you use :class:`~websockets.connect` as an asynchronous context manager,
15+
then websockets closes the connection on exit. If not, then your code is
16+
responsible for closing the connection.
17+
18+
* On the server side, :class:`~websockets.serve` starts listening for client
19+
connections and yields an server object that supports closing the server.
20+
21+
Then, when clients connects, the server initializes a connection object and
22+
passes it to a handler coroutine, which is where your code can send or
23+
receive messages. This pattern is called `inversion of control`_. It's
24+
common in frameworks implementing servers.
25+
26+
When the handler coroutine terminates, websockets closes the connection. You
27+
may also close it in the handler coroutine if you'd like.
28+
29+
.. _inversion of control: https://en.wikipedia.org/wiki/Inversion_of_control
30+
31+
Once the connection is open, the WebSocket protocol is symmetrical, except for
32+
low-level details that websockets manages under the hood. At this point,
33+
websockets provides the same API — and uses the same implementation — for
34+
client and server connections.
35+
36+
.. toctree::
37+
:maxdepth: 2
38+
39+
client
40+
server
41+
extensions
42+
utilities
43+
types
44+
45+
For convenience, public APIs can be imported from the :mod:`websockets`
46+
package, unless noted otherwise. Anything that isn't listed in this document
47+
is a private API.
48+
49+
50+
51+
52+

docs/deployment.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ On Unix systems, shutdown is usually triggered by sending a signal.
3434

3535
Here's a full example for handling SIGTERM on Unix:
3636

37-
.. literalinclude:: ../example/shutdown.py
37+
.. literalinclude:: ../example/shutdown_server.py
3838
:emphasize-lines: 13,17-19
3939

4040
This example is easily adapted to handle other signals. If you override the

docs/faq.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ See `issue 414`_.
142142

143143
.. _issue 414: https://github.com/aaugustin/websockets/issues/414
144144

145+
How do I stop a client that is continuously processing messages?
146+
................................................................
147+
148+
You can close the connection.
149+
150+
Here's an example that terminates cleanly when it receives SIGTERM on Unix:
151+
152+
.. literalinclude:: ../example/shutdown_client.py
153+
:emphasize-lines: 10-13
154+
155+
145156
How do I disable TLS/SSL certificate verification?
146157
..................................................
147158

example/shutdown_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
3+
import asyncio
4+
import signal
5+
import websockets
6+
7+
async def client():
8+
uri = "ws://localhost:8765"
9+
async with websockets.connect(uri) as websocket:
10+
# Close the connection when receiving SIGTERM.
11+
loop = asyncio.get_event_loop()
12+
loop.add_signal_handler(
13+
signal.SIGTERM, loop.create_task, websocket.close())
14+
15+
# Process messages received on the connection.
16+
async for message in websocket:
17+
...
18+
19+
asyncio.get_event_loop().run_until_complete(client())
File renamed without changes.

0 commit comments

Comments
 (0)