From fc57effd5c60ea9124598fec068cc47ac32ce061 Mon Sep 17 00:00:00 2001 From: hunicus <93150691+hunicus@users.noreply.github.com> Date: Tue, 13 Sep 2022 06:44:34 -0400 Subject: [PATCH] Add python example for websocket api docs --- .../src/app/docs/api-docs/api-docs-data.ts | 34 +++++++++++++++++++ .../code-template.component.html | 7 ++++ .../code-template/code-template.component.ts | 4 +++ 3 files changed, 45 insertions(+) diff --git a/frontend/src/app/docs/api-docs/api-docs-data.ts b/frontend/src/app/docs/api-docs/api-docs-data.ts index 7ea01584e..17fc3051b 100644 --- a/frontend/src/app/docs/api-docs/api-docs-data.ts +++ b/frontend/src/app/docs/api-docs/api-docs-data.ts @@ -62,6 +62,40 @@ console.log(res["mempool-blocks"]); } }); `, + python: `import websocket +import _thread +import time +import rel +import json + +rel.safe_read() + +def on_message(ws, message): + print(json.loads(message)) + +def on_error(ws, error): + print(error) + +def on_close(ws, close_status_code, close_msg): + print("### closed ###") + +def on_open(ws): + message = { "action": "init" } + ws.send(json.dumps(message)) + message = { "action": "want", "data": ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart', 'watch-mempool'] } + ws.send(json.dumps(message)) + +if __name__ == "__main__": + ws = websocket.WebSocketApp("wss://mempool.space/api/v1/ws", + on_open=on_open, + on_message=on_message, + on_error=on_error, + on_close=on_close) + + ws.run_forever(dispatcher=rel) # Set dispatcher to automatic reconnection + rel.signal(2, rel.abort) # Keyboard Interrupt + rel.dispatch() + `, }, codeSampleMainnet: emptyCodeSample, codeSampleTestnet: emptyCodeSample, diff --git a/frontend/src/app/docs/code-template/code-template.component.html b/frontend/src/app/docs/code-template/code-template.component.html index 373f7daf6..8fac91114 100644 --- a/frontend/src/app/docs/code-template/code-template.component.html +++ b/frontend/src/app/docs/code-template/code-template.component.html @@ -30,6 +30,13 @@
+
+