Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,12 @@ async def _websocket_handler(
protocol = request.transport.get_protocol()
ws = await protocol.websocket_handshake(request, subprotocols)

await self.dispatch(
"websocket.handler.before",
inline=True,
context={"request": request, "websocket": ws},
fail_not_found=False,
Comment thread
ahopkins marked this conversation as resolved.
)
# schedule the application handler
# its future is kept in self.websocket_tasks in case it
# needs to be cancelled due to the server being stopped
Expand All @@ -1377,10 +1383,24 @@ async def _websocket_handler(
cancelled = False
try:
await fut
await self.dispatch(
"websocket.handler.after",
inline=True,
context={"request": request, "websocket": ws},
reverse=True,
fail_not_found=False,
)
except (CancelledError, ConnectionClosed): # type: ignore
cancelled = True
except Exception as e:
self.error_handler.log(request, e)
await self.dispatch(
"websocket.handler.exception",
inline=True,
context={"request": request, "websocket": ws, "exception": e},
reverse=True,
fail_not_found=False,
)
finally:
self.websocket_tasks.remove(fut)
if cancelled:
Expand Down
8 changes: 8 additions & 0 deletions sanic/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Event(Enum):
HTTP_LIFECYCLE_SEND = "http.lifecycle.send"
HTTP_MIDDLEWARE_AFTER = "http.middleware.after"
HTTP_MIDDLEWARE_BEFORE = "http.middleware.before"
WEBSOCKET_HANDLER_AFTER = "websocket.handler.after"
WEBSOCKET_HANDLER_BEFORE = "websocket.handler.before"
WEBSOCKET_HANDLER_EXCEPTION = "websocket.handler.exception"


RESERVED_NAMESPACES = {
Expand Down Expand Up @@ -65,6 +68,11 @@ class Event(Enum):
Event.HTTP_MIDDLEWARE_AFTER.value,
Event.HTTP_MIDDLEWARE_BEFORE.value,
),
"websocket": {
Event.WEBSOCKET_HANDLER_AFTER.value,
Event.WEBSOCKET_HANDLER_BEFORE.value,
Event.WEBSOCKET_HANDLER_EXCEPTION.value,
},
}


Expand Down
62 changes: 62 additions & 0 deletions tests/test_ws_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,65 @@ async def ws_echo_handler(request: Request, ws: Websocket):
)
assert ws_proxy.client_sent == ["test 1", "test 2", ""]
assert ws_proxy.client_received == ["test 1", "test 2"]


def signalapp(app):
@app.signal("websocket.handler.before")
async def ws_before(request: Request, websocket: Websocket):
app.ctx.seq.append("before")
print("before")
await websocket.send("before: " + await websocket.recv())
print("before2")

@app.signal("websocket.handler.after")
async def ws_after(request: Request, websocket: Websocket):
app.ctx.seq.append("after")
await websocket.send("after: " + await websocket.recv())
await websocket.recv()

@app.signal("websocket.handler.exception")
async def ws_exception(
request: Request, websocket: Websocket, exception: Exception
):
app.ctx.seq.append("exception")
await websocket.send(f"exception: {exception}")
await websocket.recv()

@app.websocket("/ws")
async def ws_handler(request: Request, ws: Websocket):
app.ctx.seq.append("ws")

@app.websocket("/wserror")
async def ws_error(request: Request, ws: Websocket):
print("wserr")
app.ctx.seq.append("wserror")
raise Exception(await ws.recv())
print("wserr2")


def test_ws_signals(
app: Sanic,
simple_ws_mimic_client: MimicClientType,
):
signalapp(app)

app.ctx.seq = []
_, ws_proxy = app.test_client.websocket(
"/ws", mimic=simple_ws_mimic_client
)
assert ws_proxy.client_received == ["before: test 1", "after: test 2"]
assert app.ctx.seq == ["before", "ws", "after"]


def test_ws_signals_exception(
app: Sanic,
simple_ws_mimic_client: MimicClientType,
):
signalapp(app)

app.ctx.seq = []
_, ws_proxy = app.test_client.websocket(
"/wserror", mimic=simple_ws_mimic_client
)
assert ws_proxy.client_received == ["before: test 1", "exception: test 2"]
assert app.ctx.seq == ["before", "wserror", "exception"]