Permalink
Switch branches/tags
Nothing to show
Find file Copy path
4097835 Oct 14, 2017
1 contributor

Users who have contributed to this file

43 lines (30 sloc) 1009 Bytes
import asyncio
import os
import aiohttp.web
HOST = os.getenv('HOST', '0.0.0.0')
PORT = int(os.getenv('PORT', 8080))
async def testhandle(request):
return aiohttp.web.Response(text='Test handle')
async def websocket_handler(request):
print('Websocket connection starting')
ws = aiohttp.web.WebSocketResponse()
await ws.prepare(request)
print('Websocket connection ready')
async for msg in ws:
print(msg)
if msg.type == aiohttp.WSMsgType.TEXT:
print(msg.data)
if msg.data == 'close':
await ws.close()
else:
await ws.send_str(msg.data + '/answer')
print('Websocket connection closed')
return ws
def main():
loop = asyncio.get_event_loop()
app = aiohttp.web.Application(loop=loop)
app.router.add_route('GET', '/', testhandle)
app.router.add_route('GET', '/ws', websocket_handler)
aiohttp.web.run_app(app, host=HOST, port=PORT)
if __name__ == '__main__':
main()