diff options
author | engn33r <engn33r@users.noreply.github.com> | 2021-12-28 18:50:45 -0500 |
---|---|---|
committer | engn33r <engn33r@users.noreply.github.com> | 2021-12-28 18:50:45 -0500 |
commit | cd3ed9568d230be9c84631f34e96c9f354f62a80 (patch) | |
tree | 50c803e4e0f029e7e4db4663ef1d928a97b40f35 /docs | |
parent | a28a016584f82cfca30a09a59ecd764569c2fa3f (diff) | |
download | websocket-client-cd3ed9568d230be9c84631f34e96c9f354f62a80.tar.gz |
Add with statement example to fix #772
Diffstat (limited to 'docs')
-rw-r--r-- | docs/source/examples.rst | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/docs/source/examples.rst b/docs/source/examples.rst index 29caf1c..18016be 100644 --- a/docs/source/examples.rst +++ b/docs/source/examples.rst @@ -94,6 +94,33 @@ The output you will see will look something like this: send: b'\x88\x82 \xc3\x85E#+' +Using websocket-client with "with" statements +============================================== + +It is possible to use "with" statements, as outlined in PEP 343, to help +manage the closing of WebSocket connections after a message is received. +Below is one example of this being done with a short-lived connection: + +**Short-lived WebSocket using "with" statement** + +:: + + from websocket import create_connection + from contextlib import contextmanager + + @contextmanager + def closing(thing): + try: + yield thing + finally: + thing.close() + + with closing(create_connection("wss://stream.meetup.com/2/rsvps")) as conn: + print(conn.recv()) + + # Connection is now closed + + Connection Options =================== |