Skip to content

Commit

Permalink
Add example of client shutdown.
Browse files Browse the repository at this point in the history
Fix #933.
  • Loading branch information
aaugustin committed May 1, 2021
1 parent 2080026 commit 87a7302
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
52 changes: 52 additions & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
API
===

``websockets`` provides complete client and server implementations, as shown
in the :doc:`getting started guide <intro>`.

The process for opening and closing a WebSocket connection depends on which
side you're implementing.

* On the client side, connecting to a server with :class:`~websockets.connect`
yields a connection object that provides methods for interacting with the
connection. Your code can open a connection, then send or receive messages.

If you use :class:`~websockets.connect` as an asynchronous context manager,
then websockets closes the connection on exit. If not, then your code is
responsible for closing the connection.

* On the server side, :class:`~websockets.serve` starts listening for client
connections and yields an server object that supports closing the server.

Then, when clients connects, the server initializes a connection object and
passes it to a handler coroutine, which is where your code can send or
receive messages. This pattern is called `inversion of control`_. It's
common in frameworks implementing servers.

When the handler coroutine terminates, websockets closes the connection. You
may also close it in the handler coroutine if you'd like.

.. _inversion of control: https://en.wikipedia.org/wiki/Inversion_of_control

Once the connection is open, the WebSocket protocol is symmetrical, except for
low-level details that websockets manages under the hood. At this point,
websockets provides the same API — and uses the same implementation — for
client and server connections.

.. toctree::
:maxdepth: 2

client
server
extensions
utilities
types

For convenience, public APIs can be imported from the :mod:`websockets`
package, unless noted otherwise. Anything that isn't listed in this document
is a private API.





2 changes: 1 addition & 1 deletion docs/deployment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ On Unix systems, shutdown is usually triggered by sending a signal.

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

.. literalinclude:: ../example/shutdown.py
.. literalinclude:: ../example/shutdown_server.py
:emphasize-lines: 13,17-19

This example is easily adapted to handle other signals. If you override the
Expand Down
11 changes: 11 additions & 0 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ See `issue 414`_.

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

How do I stop a client that is continuously processing messages?
................................................................

You can close the connection.

Here's an example that terminates cleanly when it receives SIGTERM on Unix:

.. literalinclude:: ../example/shutdown_client.py
:emphasize-lines: 10-13


How do I disable TLS/SSL certificate verification?
..................................................

Expand Down
19 changes: 19 additions & 0 deletions example/shutdown_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python

import asyncio
import signal
import websockets

async def client():
uri = "ws://localhost:8765"
async with websockets.connect(uri) as websocket:
# Close the connection when receiving SIGTERM.
loop = asyncio.get_event_loop()
loop.add_signal_handler(
signal.SIGTERM, loop.create_task, websocket.close())

# Process messages received on the connection.
async for message in websocket:
...

asyncio.get_event_loop().run_until_complete(client())
File renamed without changes.

0 comments on commit 87a7302

Please sign in to comment.