aio-libs / aiohttp-sse

Server-sent events support for aiohttp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No jQuery example

blopker opened this issue · comments

Hey, just getting into aiohttp. Nice project!

Anyway, I saw the example and thought users could do without the jQuery. Here's the example without it, plus I changed /index to just / because that tripped me up when I first ran it.

import asyncio
from aiohttp import web
from aiohttp.web import Response
from aiohttp_sse import sse_response
from datetime import datetime


async def hello(request):
    loop = request.app.loop
    async with sse_response(request) as resp:
        while True:
            data = 'Server Time : {}'.format(datetime.now())
            print(data)
            await resp.send(data)
            await asyncio.sleep(1, loop=loop)
    return resp


async def index(request):
    d = """
        <html>
        <body>
            <script>
                var evtSource = new EventSource("/hello");
                evtSource.onmessage = function(e) {
                    document.getElementById('response').innerText = e.data
                }
            </script>
            <h1>Response from server:</h1>
            <div id="response"></div>
        </body>
    </html>
    """
    return Response(text=d, content_type='text/html')


app = web.Application()
app.router.add_route('GET', '/hello', hello)
app.router.add_route('GET', '/', index)
web.run_app(app, host='127.0.0.1', port=8080)

If you don't want to change it, feel free to just close this.

Cheers!

Thanks! Can you please create PR with proposed changes?

Done!