Skip to content

Instantly share code, notes, and snippets.

@kobayashi
Created March 19, 2019 04:28
Show Gist options
  • Save kobayashi/2138c60ab400f5a31561359fad49b8a6 to your computer and use it in GitHub Desktop.
Save kobayashi/2138c60ab400f5a31561359fad49b8a6 to your computer and use it in GitHub Desktop.
responder with websocket
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import asyncio
import websockets
async def client():
async with websockets.connect('ws://127.0.0.1:5042/ws') as websocket:
await websocket.send("KOBAYASHI")
response = await websocket.recv()
print(response)
asyncio.get_event_loop().run_until_complete(client())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import responder
api = responder.API()
@api.route('/ws', websocket=True)
async def websocket(ws):
def sleep(name, s=5):
time.sleep(s)
return "Hello {}".format(name)
await ws.accept()
while True:
name = await ws.receive_text()
re = sleep(name)
await ws.send_text(re)
await ws.close()
if __name__ == '__main__':
api.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment