diff --git a/examples/sse/counter.py b/examples/sse/counter.py index 36f4bec..ee5a536 100644 --- a/examples/sse/counter.py +++ b/examples/sse/counter.py @@ -1,16 +1,28 @@ import asyncio -from microdot import Microdot +from microdot import Microdot, send_file from microdot.sse import with_sse app = Microdot() +@app.route("/") +async def main(request): + return send_file('index.html') + + @app.route('/events') @with_sse async def events(request, sse): - for i in range(10): - await asyncio.sleep(1) - await sse.send({'counter': i}) + print('Client connected') + try: + i = 0 + while True: + await asyncio.sleep(1) + i += 1 + await sse.send({'counter': i}) + except asyncio.CancelledError: + pass + print('Client disconnected') -app.run(debug=True) +app.run() diff --git a/examples/sse/index.html b/examples/sse/index.html new file mode 100644 index 0000000..bc64df2 --- /dev/null +++ b/examples/sse/index.html @@ -0,0 +1,30 @@ + + + + Microdot SSE Example + + + +

Microdot SSE Example

+
+ + +