summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAndrey Bashlakov <a.bashlakov@corp.vk.com>2018-06-18 16:00:32 +0300
committerAndrey Bashlakov <a.bashlakov@corp.vk.com>2018-06-18 16:00:32 +0300
commitf9de412ed5f0f56bd317bff954e10928044e90da (patch)
treef37219fbb2cc50b4303dc966d9a0228084c98fb0 /examples
parentdf275d351f9887fba2774e2e1aa79ff1e5a24bd1 (diff)
downloadwebsocket-client-f9de412ed5f0f56bd317bff954e10928044e90da.tar.gz
Patch WebSocketApp class to make it inheritable
Diffstat (limited to 'examples')
-rw-r--r--examples/echoapp_client_inheritance.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/examples/echoapp_client_inheritance.py b/examples/echoapp_client_inheritance.py
new file mode 100644
index 0000000..775e632
--- /dev/null
+++ b/examples/echoapp_client_inheritance.py
@@ -0,0 +1,40 @@
+import websocket
+from threading import Thread
+import time
+import sys
+
+
+class MyApp(websocket.WebSocketApp):
+ def on_message(self, message):
+ print(message)
+
+ def on_error(self, error):
+ print(error)
+
+ def on_close(self):
+ print("### closed ###")
+
+ def on_open(self):
+ def run(*args):
+ for i in range(3):
+ # send the message, then wait
+ # so thread doesn't exit and socket
+ # isn't closed
+ self.send("Hello %d" % i)
+ time.sleep(1)
+
+ time.sleep(1)
+ self.close()
+ print("Thread terminating...")
+
+ Thread(target=run).start()
+
+
+if __name__ == "__main__":
+ websocket.enableTrace(True)
+ if len(sys.argv) < 2:
+ host = "ws://echo.websocket.org/"
+ else:
+ host = sys.argv[1]
+ ws = MyApp(host)
+ ws.run_forever()