mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-12-12 07:08:55 +00:00
08895e9ba6
- restructure examples (fix #4031) - remove example dependencies from setup.py, we do not need special dependencies for our supported addons. - unify how we generate docs from code - improve example docs
25 lines
738 B
Python
25 lines
738 B
Python
"""
|
|
Inject a WebSocket message into a running connection.
|
|
|
|
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, f'This is the #{i} injected message!')
|
|
i += 1
|
|
|
|
def websocket_start(self, flow):
|
|
asyncio.get_event_loop().create_task(self.inject(flow))
|
|
|
|
|
|
addons = [InjectWebSocketMessage()]
|