summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorengn33r <engn33r@users.noreply.github.com>2021-12-15 07:26:09 -0800
committerengn33r <engn33r@users.noreply.github.com>2021-12-15 07:26:09 -0800
commit5ea9af47425fcd4d6837dd7311d4b52ca0955e8e (patch)
treec94fc13a25844ac4a75bebe15041efef7ca13614
parent51e87808fb70a2587620e69378aae6e595578076 (diff)
downloadwebsocket-client-5ea9af47425fcd4d6837dd7311d4b52ca0955e8e.tar.gz
Add dispatcher to run_forever examples to fix #580
-rw-r--r--README.md30
-rw-r--r--docs/source/faq.rst3
2 files changed, 15 insertions, 18 deletions
diff --git a/README.md b/README.md
index 04c2bd9..b7f55fd 100644
--- a/README.md
+++ b/README.md
@@ -68,13 +68,17 @@ Many more examples are found in the
### Long-lived Connection
Most real-world WebSockets situations involve longer-lived connections.
-The WebSocketApp `run_forever` loop automatically tries to reconnect when a
-connection is lost, and provides a variety of event-based connection controls.
+The WebSocketApp `run_forever` loop will automatically try to reconnect when a
+connection is lost if it is provided with a dispatcher parameter,
+and provides a variety of event-based connection controls.
```python
import websocket
import _thread
import time
+import rel
+
+rel.safe_read()
def on_message(ws, message):
print(message)
@@ -86,24 +90,19 @@ def on_close(ws, close_status_code, close_msg):
print("### closed ###")
def on_open(ws):
- def run(*args):
- for i in range(3):
- time.sleep(1)
- ws.send("Hello %d" % i)
- time.sleep(1)
- ws.close()
- print("thread terminating...")
- _thread.start_new_thread(run, ())
+ print("Opened connection")
if __name__ == "__main__":
websocket.enableTrace(True)
- ws = websocket.WebSocketApp("ws://echo.websocket.org/",
+ ws = websocket.WebSocketApp("wss://api.gemini.com/v1/marketdata/BTCUSD",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
- ws.run_forever()
+ ws.run_forever(dispatcher=rel) # Set dispatcher to automatic reconnection
+ rel.signal(2, rel.abort) # Keyboard Interrupt
+ rel.dispatch()
```
### Short-lived Connection
@@ -114,6 +113,7 @@ server is running and responds properly to a specific request.
```python
from websocket import create_connection
+
ws = create_connection("ws://echo.websocket.org/")
print("Sending 'Hello, World'...")
ws.send("Hello, World")
@@ -128,11 +128,7 @@ If you want to customize socket options, set sockopt, as seen below:
```python
from websocket import create_connection
+
ws = create_connection("ws://echo.websocket.org/",
sockopt=((socket.IPPROTO_TCP, socket.TCP_NODELAY),))
```
-
-### Acknowledgements
-
-Thanks to @battlemidget and @ralphbean for helping migrate this project to
-Python 3.
diff --git a/docs/source/faq.rst b/docs/source/faq.rst
index ed909ad..b16bcbf 100644
--- a/docs/source/faq.rst
+++ b/docs/source/faq.rst
@@ -62,7 +62,8 @@ connection is closed when you try to use it. In order to properly carry out
further functions with your WebSocket connection after the connection has
closed, you will need to reconnect the WebSocket, using ``connect()`` or
``create_connection()`` (from the _core.py file). The WebSocketApp ``run_forever()``
-function automatically tries to reconnect when the connection is lost.
+function automatically tries to reconnect when the connection is lost
+if a dispatcher parameter is provided to the ``run_forever()`` function.
What's going on with the naming of this library?
==================================================