|
7 | 7 | from typing import Optional |
8 | 8 |
|
9 | 9 | from integration_tests.subroutes import di_subrouter, sub_router |
10 | | -from robyn import Headers, Request, Response, Robyn, SSEMessage, SSEResponse, WebSocket, WebSocketConnector, jsonify, serve_file, serve_html |
| 10 | +from robyn import Headers, Request, Response, Robyn, SSEMessage, SSEResponse, WebSocketConnector, WebSocketDisconnect, jsonify, serve_file, serve_html |
11 | 11 | from robyn.authentication import AuthenticationHandler, BearerGetter, Identity |
12 | 12 | from robyn.robyn import QueryParams, Url |
13 | 13 | from robyn.templating import JinjaTemplate |
14 | 14 | from robyn.types import Body, JSONResponse, Method, PathParams |
15 | 15 |
|
16 | 16 | app = Robyn(__file__) |
17 | | -websocket = WebSocket(app, "/web_socket") |
18 | | - |
19 | | -# Creating a new WebSocket app to test json handling + to serve an example to future users of this lib |
20 | | -# while the original "raw" web_socket is used with benchmark tests |
21 | | -websocket_json = WebSocket(app, "/web_socket_json") |
22 | | - |
23 | | -websocket_di = WebSocket(app, "/web_socket_di") |
24 | | - |
25 | | -websocket_di.inject_global(GLOBAL_DEPENDENCY="GLOBAL DEPENDENCY") |
26 | | -websocket_di.inject(ROUTER_DEPENDENCY="ROUTER DEPENDENCY") |
27 | 17 |
|
28 | 18 | current_file_path = pathlib.Path(__file__).parent.resolve() |
29 | 19 | jinja_template = JinjaTemplate(os.path.join(current_file_path, "templates")) |
|
34 | 24 | websocket_state = defaultdict(int) |
35 | 25 |
|
36 | 26 |
|
37 | | -@websocket_json.on("message") |
38 | | -async def jsonws_message(ws, msg: str) -> str: |
39 | | - websocket_id = ws.id |
40 | | - response: dict = {"ws_id": websocket_id, "resp": "", "msg": msg} |
41 | | - global websocket_state |
42 | | - state = websocket_state[websocket_id] |
43 | | - if state == 0: |
44 | | - response["resp"] = "Whaaat??" |
45 | | - elif state == 1: |
46 | | - response["resp"] = "Whooo??" |
47 | | - elif state == 2: |
48 | | - response["resp"] = "*chika* *chika* Slim Shady." |
49 | | - websocket_state[websocket_id] = (state + 1) % 3 |
50 | | - return jsonify(response) |
51 | | - |
52 | | - |
53 | | -@websocket.on("message") |
54 | | -async def message(ws: WebSocketConnector, msg: str, global_dependencies) -> str: |
55 | | - global websocket_state |
56 | | - websocket_id = ws.id |
57 | | - state = websocket_state[websocket_id] |
58 | | - resp = "" |
59 | | - if state == 0: |
60 | | - resp = "Whaaat??" |
61 | | - await ws.async_broadcast("This is a broadcast message") |
62 | | - ws.sync_send_to(websocket_id, "This is a message to self") |
63 | | - elif state == 1: |
64 | | - resp = "Whooo??" |
65 | | - elif state == 2: |
66 | | - await ws.async_broadcast(ws.query_params.get("one", None)) |
67 | | - ws.sync_send_to(websocket_id, ws.query_params.get("two", None)) |
68 | | - resp = "*chika* *chika* Slim Shady." |
69 | | - elif state == 3: |
70 | | - ws.close() |
71 | | - # TODO temporary fix to avoid CI failure |
72 | | - resp = "Connection closed" |
73 | | - |
74 | | - websocket_state[websocket_id] = (state + 1) % 4 |
75 | | - return resp |
76 | | - |
77 | | - |
78 | | -@websocket.on("close") |
79 | | -def close(): |
80 | | - return "GoodBye world, from ws" |
| 27 | +# Regular WebSocket endpoint |
| 28 | +@app.websocket("/web_socket") |
| 29 | +async def websocket_endpoint(websocket): |
| 30 | + await websocket.accept() |
| 31 | + |
| 32 | + try: |
| 33 | + while True: |
| 34 | + msg = await websocket.receive_text() |
| 35 | + websocket_id = websocket._connector.id |
| 36 | + global websocket_state |
| 37 | + state = websocket_state[websocket_id] |
| 38 | + |
| 39 | + if state == 0: |
| 40 | + await websocket._connector.async_broadcast("This is a broadcast message") |
| 41 | + await websocket.send_text("This is a message to self") |
| 42 | + await websocket.send_text("Whaaat??") |
| 43 | + elif state == 1: |
| 44 | + await websocket.send_text("Whooo??") |
| 45 | + elif state == 2: |
| 46 | + await websocket._connector.async_broadcast(websocket.query_params.get("one", "")) |
| 47 | + await websocket.send_text(websocket.query_params.get("two", "")) |
| 48 | + await websocket.send_text("*chika* *chika* Slim Shady.") |
| 49 | + elif state == 3: |
| 50 | + await websocket.send_text("Connection closed") |
| 51 | + await websocket.close() |
| 52 | + break |
| 53 | + |
| 54 | + websocket_state[websocket_id] = (state + 1) % 4 |
| 55 | + |
| 56 | + except WebSocketDisconnect: |
| 57 | + pass |
| 58 | + |
| 59 | + |
| 60 | +@websocket_endpoint.on_connect |
| 61 | +def websocket_on_connect(websocket): |
| 62 | + return "Hello world, from ws" |
81 | 63 |
|
82 | 64 |
|
83 | | -@websocket_json.on("close") |
84 | | -def jsonws_close(): |
| 65 | +@websocket_endpoint.on_close |
| 66 | +def websocket_on_close(websocket): |
85 | 67 | return "GoodBye world, from ws" |
86 | 68 |
|
87 | 69 |
|
88 | | -@websocket.on("connect") |
89 | | -def connect(): |
| 70 | +# JSON WebSocket endpoint |
| 71 | +@app.websocket("/web_socket_json") |
| 72 | +async def json_websocket_endpoint(websocket): |
| 73 | + await websocket.accept() |
| 74 | + |
| 75 | + try: |
| 76 | + while True: |
| 77 | + msg = await websocket.receive_text() |
| 78 | + websocket_id = websocket._connector.id |
| 79 | + response = {"ws_id": websocket_id, "resp": "", "msg": msg} |
| 80 | + global websocket_state |
| 81 | + state = websocket_state[websocket_id] |
| 82 | + |
| 83 | + if state == 0: |
| 84 | + response["resp"] = "Whaaat??" |
| 85 | + elif state == 1: |
| 86 | + response["resp"] = "Whooo??" |
| 87 | + elif state == 2: |
| 88 | + response["resp"] = "*chika* *chika* Slim Shady." |
| 89 | + |
| 90 | + websocket_state[websocket_id] = (state + 1) % 3 |
| 91 | + await websocket.send_json(response) |
| 92 | + |
| 93 | + except WebSocketDisconnect: |
| 94 | + pass |
| 95 | + |
| 96 | + |
| 97 | +@json_websocket_endpoint.on_connect |
| 98 | +def json_websocket_on_connect(websocket): |
90 | 99 | return "Hello world, from ws" |
91 | 100 |
|
92 | 101 |
|
93 | | -@websocket_json.on("connect") |
94 | | -def jsonws_connect(): |
95 | | - return "Hello world, from ws" |
| 102 | +@json_websocket_endpoint.on_close |
| 103 | +def json_websocket_on_close(websocket): |
| 104 | + return "GoodBye world, from ws" |
96 | 105 |
|
97 | 106 |
|
98 | | -@websocket_di.on("connect") |
99 | | -async def di_message_connect(global_dependencies, router_dependencies): |
100 | | - return global_dependencies["GLOBAL_DEPENDENCY"] + " " + router_dependencies["ROUTER_DEPENDENCY"] |
| 107 | +# WebSocket with dependency injection |
| 108 | +@app.websocket("/web_socket_di") |
| 109 | +async def di_websocket_endpoint(websocket): |
| 110 | + await websocket.accept() |
| 111 | + |
| 112 | + try: |
| 113 | + while True: |
| 114 | + await websocket.receive_text() |
| 115 | + # Just echo back an empty response for DI test |
| 116 | + await websocket.send_text("") |
| 117 | + |
| 118 | + except WebSocketDisconnect: |
| 119 | + pass |
101 | 120 |
|
102 | 121 |
|
103 | | -@websocket_di.on("message") |
104 | | -async def di_message(): |
105 | | - return "" |
| 122 | +@di_websocket_endpoint.on_connect |
| 123 | +async def di_websocket_on_connect(websocket): |
| 124 | + # Simulating dependency injection |
| 125 | + global_dep = "GLOBAL DEPENDENCY" |
| 126 | + router_dep = "ROUTER DEPENDENCY" |
| 127 | + return f"{global_dep} {router_dep}" |
106 | 128 |
|
107 | 129 |
|
108 | | -@websocket_di.on("close") |
109 | | -async def di_message_close(): |
| 130 | +@di_websocket_endpoint.on_close |
| 131 | +async def di_websocket_on_close(websocket): |
110 | 132 | return "" |
111 | 133 |
|
112 | 134 |
|
|
0 commit comments