sanic/examples/ws.py

22 lines
440 B
Python
Raw Normal View History

2017-02-10 07:36:15 +00:00
import asyncio
import sanic
from sanic.response import html
import datetime
import random
app = sanic.Sanic()
async def time(websocket, path):
while True:
now = datetime.datetime.utcnow().isoformat() + 'Z'
await websocket.send(now)
await asyncio.sleep(random.random() * 3)
app.websocket(time, 'localhost', 3000)
@app.route('/')
def hello(request):
return html(open('ws.html').read())
app.run(port=8000)