user185887
user185887

Reputation: 713

How does websockets library in python work

I am using websocket library in python for one of my projects. It works for me but I am curious to know how it works. I could not find this in the documentation. Specifically, for the example given in the docs,

#!/usr/bin/env python

# WS server example that synchronizes state across clients

import asyncio
import json
import logging
import websockets

logging.basicConfig()

STATE = {"value": 0}

USERS = set()


def state_event():
    return json.dumps({"type": "state", **STATE})


def users_event():
    return json.dumps({"type": "users", "count": len(USERS)})


async def notify_state():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = state_event()
        await asyncio.wait([user.send(message) for user in USERS])


async def notify_users():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = users_event()
        await asyncio.wait([user.send(message) for user in USERS])


async def register(websocket):
    USERS.add(websocket)
    await notify_users()


async def unregister(websocket):
    USERS.remove(websocket)
    await notify_users()


async def counter(websocket, path):
    # register(websocket) sends user_event() to websocket
    await register(websocket)
    try:
        await websocket.send(state_event())
        async for message in websocket:
            data = json.loads(message)
            if data["action"] == "minus":
                STATE["value"] -= 1
                await notify_state()
            elif data["action"] == "plus":
                STATE["value"] += 1
                await notify_state()
            else:
                logging.error("unsupported event: {}", data)
    finally:
        await unregister(websocket)


start_server = websockets.serve(counter, "localhost", 6789)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

Whenever, I disconnect, the event unregister() gets fired. How does websocket come to know that I have disconnected? My guess is the line async for message in websocket: has to do something with it but I do not know the details. Any insight on it will be appreciated.

Upvotes: 0

Views: 363

Answers (1)

mikesim1
mikesim1

Reputation: 137

With the websockets module, disconnections are handled as exceptions. For example, if you wanted to handle different kinds of disconnections, you could do something like this:

try:
    await websocket.send(state_event())
        async for message in websocket:
            # Your code...
except websockets.exceptions.ConnectionClosedOK:
    print("Client disconnected OK")
except websockets.exceptions.ConnectionClosedError:
    print("Client disconnected unexpectedly")
finally:
    await unregister(websocket)

More information can be found in the documentation.

Upvotes: 2

Related Questions