0
votes

I have to trigger some actions after the connection closes. This is the code

from sanic import response
from sanic.websocket import ConnectionClosed

@app.websocket('/tablefeed')
async def feed(request, ws):
    try:
        while True:
            client_data = json.loads(await ws.recv())
            client_code = client_data.get('id')
            if client_code == 1: "do something"
    except ConnectionClosed:
        "do something else"

The "do something else" doesn't execute when client closes their connection. How can I get notified when the connection is lost?

1

1 Answers

0
votes

You can do something like that:

from sanic import response
from sanic.websocket import ConnectionClosed

@app.websocket('/tablefeed')
async def feed(request, ws):
    try:
        while True:
            client_data = json.loads(await ws.recv())
            client_code = client_data.get('id')
            if client_code == 1: "do something"
    except Exception as e:
        print("Some general error")
    except:
        print("Websocket disconnect")