Last active
February 6, 2019 20:02
-
-
Save ankitml/1baeee81dd8635ef43259e5f0ec81ebe to your computer and use it in GitHub Desktop.
Websockets Tutorial 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import websockets | |
async def ws_handler(websocket, path): | |
async for message in websocket: | |
print(message) | |
if message == 'ping': | |
await websocket.send('pong') | |
# initialize websocket server from this handler | |
if __name__ == '__main__': | |
asyncio.get_event_loop().run_until_complete( | |
websockets.serve(ws_handler, '0.0.0.0', 9999), | |
) | |
asyncio.get_event_loop().run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment