aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorThomas Kriechbaumer <thomas@kriechbaumer.name>2018-05-12 14:04:47 +0200
committerThomas Kriechbaumer <thomas@kriechbaumer.name>2018-05-16 21:50:15 +0200
commit976ab0c46696ea44b4a30856f9ac1fed4acb4a25 (patch)
tree9542cdc29ea3989ada11c471adc90ed732c4a44a /examples
parenta38497ac36aa72f0e431205d9c2b0394368d0810 (diff)
downloadmitmproxy-976ab0c46696ea44b4a30856f9ac1fed4acb4a25.tar.gz
mitmproxy-976ab0c46696ea44b4a30856f9ac1fed4acb4a25.tar.bz2
mitmproxy-976ab0c46696ea44b4a30856f9ac1fed4acb4a25.zip
websocket: inject messages via flow
Diffstat (limited to 'examples')
-rw-r--r--examples/complex/websocket_inject_message.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/examples/complex/websocket_inject_message.py b/examples/complex/websocket_inject_message.py
new file mode 100644
index 00000000..e9c3ea0c
--- /dev/null
+++ b/examples/complex/websocket_inject_message.py
@@ -0,0 +1,23 @@
+"""
+This example shows how to inject a WebSocket message to the client.
+Every new WebSocket connection will trigger a new asyncio task that
+periodically injects a new message to the client.
+"""
+import asyncio
+import mitmproxy.websocket
+
+
+class InjectWebSocketMessage:
+
+ async def inject(self, flow: mitmproxy.websocket.WebSocketFlow):
+ i = 0
+ while not flow.ended and not flow.error:
+ await asyncio.sleep(5)
+ flow.inject_message(flow.client_conn, 'This is the #{} an injected message!'.format(i))
+ i += 1
+
+ def websocket_start(self, flow):
+ asyncio.get_event_loop().create_task(self.inject(flow))
+
+
+addons = [InjectWebSocketMessage()]