|  | 
|  | 1 | +Routing | 
|  | 2 | +======= | 
|  | 3 | + | 
|  | 4 | +.. currentmodule:: websockets | 
|  | 5 | + | 
|  | 6 | +Many WebSocket servers provide just one endpoint. That's why | 
|  | 7 | +:func:`~asyncio.server.serve` accepts a single connection handler as its first | 
|  | 8 | +argument. | 
|  | 9 | + | 
|  | 10 | +This may come as a surprise to you if you're used to HTTP servers. In a standard | 
|  | 11 | +HTTP application, each request gets dispatched to a handler based on the request | 
|  | 12 | +path. Clients know which path to use for which operation. | 
|  | 13 | + | 
|  | 14 | +In a WebSocket application, clients open a persistent connection then they send | 
|  | 15 | +all messages over that unique connection. When different messages correspond to | 
|  | 16 | +different operations, they must be dispatched based on the message content. | 
|  | 17 | + | 
|  | 18 | +Simple routing | 
|  | 19 | +-------------- | 
|  | 20 | + | 
|  | 21 | +If you need different handlers for different clients or different use cases, you | 
|  | 22 | +may route each connection to the right handler based on the request path. | 
|  | 23 | + | 
|  | 24 | +Since WebSocket servers typically provide fewer routes than HTTP servers, you | 
|  | 25 | +can keep it simple:: | 
|  | 26 | + | 
|  | 27 | + async def handler(websocket): | 
|  | 28 | + match websocket.request.path: | 
|  | 29 | + case "/blue": | 
|  | 30 | + await blue_handler(websocket) | 
|  | 31 | + case "/green": | 
|  | 32 | + await green_handler(websocket) | 
|  | 33 | + case _: | 
|  | 34 | + # No handler for this path. Close the connection. | 
|  | 35 | + return | 
|  | 36 | + | 
|  | 37 | +You may also route connections based on the first message received from the | 
|  | 38 | +client, as demonstrated in the :doc:`tutorial <../intro/tutorial2>`:: | 
|  | 39 | + | 
|  | 40 | + import json | 
|  | 41 | + | 
|  | 42 | + async def handler(websocket): | 
|  | 43 | + message = await websocket.recv() | 
|  | 44 | + settings = json.loads(message) | 
|  | 45 | + match settings["color"]: | 
|  | 46 | + case "blue": | 
|  | 47 | + await blue_handler(websocket) | 
|  | 48 | + case "green": | 
|  | 49 | + await green_handler(websocket) | 
|  | 50 | + case _: | 
|  | 51 | + # No handler for this message. Close the connection. | 
|  | 52 | + return | 
|  | 53 | + | 
|  | 54 | +When you need to authenticate the connection before routing it, this pattern is | 
|  | 55 | +more convenient. | 
|  | 56 | + | 
|  | 57 | +Complex routing | 
|  | 58 | +--------------- | 
|  | 59 | + | 
|  | 60 | +If you have outgrow these simple patterns, websockets provides full-fledged | 
|  | 61 | +routing based on the request path with :func:`~asyncio.router.route`. | 
|  | 62 | + | 
|  | 63 | +This feature builds upon Flask_'s router. To use it, you must install the | 
|  | 64 | +third-party library `werkzeug`_:: | 
|  | 65 | + | 
|  | 66 | + $ pip install werkzeug | 
|  | 67 | + | 
|  | 68 | +.. _Flask: https://flask.palletsprojects.com/ | 
|  | 69 | +.. _werkzeug: https://werkzeug.palletsprojects.com/ | 
|  | 70 | + | 
|  | 71 | +:func:`~asyncio.router.route` expects a :class:`werkzeug.routing.Map` as its | 
|  | 72 | +first argument to declare which URL patterns map to which handlers. Review the | 
|  | 73 | +documentation of :mod:`werkzeug.routing` to learn about its functionality. | 
|  | 74 | + | 
|  | 75 | +To give you a sense of what's possible, here's the URL map of the example in | 
|  | 76 | +`example/routing.py`_: | 
|  | 77 | + | 
|  | 78 | +.. _example/routing.py: https://github.com/python-websockets/websockets/blob/main/example/routing.py | 
|  | 79 | + | 
|  | 80 | +.. literalinclude:: ../../example/routing.py | 
|  | 81 | + :language: python | 
|  | 82 | + :start-at: url_map = Map( | 
|  | 83 | + :end-at: await server.serve_forever() | 
0 commit comments