Skip to content

Commit 7204db6

Browse files
feat(routing): enhance WebSocket route registration
- Allow registering WebSocket routes with pre-constructed WebsocketRoutes instances - Enable direct registration using path and handler arguments - Add middleware support for WebSocket routes - Update method signature and docstring for clarity fixes:#191
1 parent 6f590be commit 7204db6

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

nexios/routing/websocket.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,30 @@ def add_ws_route(
109109
This method registers a WebSocket route, allowing the application to handle WebSocket connections.
110110
111111
Args:
112-
route (Routes): The WebSocket route configuration.
112+
route: Either a pre-constructed WebsocketRoutes instance or None
113+
path: The URL path for the WebSocket route (required if route is None)
114+
handler: The WebSocket handler function (required if route is None)
115+
middleware: List of middleware for this route
113116
114117
Returns:
115118
None
116119
117120
Example:
118121
```python
119-
route = Routes("/ws/chat", chat_handler)
122+
# Using with a pre-constructed route
123+
route = WebsocketRoutes("/ws/chat", chat_handler)
120124
app.add_ws_route(route)
125+
126+
# Or directly with path and handler
127+
app.add_ws_route(path="/ws/chat", handler=chat_handler)
121128
```
122129
"""
123-
if route:
124-
self.add_ws_route(route)
130+
if route is not None:
131+
self.routes.append(route)
132+
elif path is not None and handler is not None:
133+
self.routes.append(WebsocketRoutes(path, handler, middleware=middleware))
125134
else:
126-
self.add_ws_route(
127-
WebsocketRoutes(path, handler, middleware=middleware)
128-
)
135+
raise ValueError("Either route or both path and handler must be provided")
129136

130137
def add_ws_middleware(self, middleware: type[ASGIApp]) -> None: # type: ignore[override]
131138
"""Add middleware to the WebSocket router"""

0 commit comments

Comments
 (0)