Snippets

emptypage WebSocket sample

Created by Masaaki Shibata
import asyncio
from websockets.server import serve
from websockets.legacy.protocol import WebSocketCommonProtocol
from websockets.exceptions import ConnectionClosed

async def echo(websocket: WebSocketCommonProtocol):
    while True:
        try:
            await websocket.send('Hello, world')
        except ConnectionClosed:
            break
        await asyncio.sleep(1.0)


async def main():
    async with serve(echo, "localhost", 8765):
        await asyncio.Future()

asyncio.run(main())

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.