Skip to content

Commit d92737a

Browse files
committed
added fastapi example
1 parent 9f970a5 commit d92737a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

examples/server_fastapi_websocket.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import uvicorn
2+
from fastapi import FastAPI, WebSocket
3+
4+
from rsocket.helpers import create_future
5+
from rsocket.local_typing import Awaitable
6+
from rsocket.payload import Payload
7+
from rsocket.request_handler import BaseRequestHandler
8+
from rsocket.rsocket_server import RSocketServer
9+
from rsocket.transports.http3_transport import Http3TransportWebsocket
10+
11+
app = FastAPI()
12+
13+
14+
class Handler(BaseRequestHandler):
15+
16+
async def request_response(self, payload: Payload) -> Awaitable[Payload]:
17+
return create_future(Payload(b'pong'))
18+
19+
20+
@app.websocket("/")
21+
async def endpoint(websocket: WebSocket):
22+
await websocket.accept()
23+
transport = Http3TransportWebsocket(websocket)
24+
RSocketServer(transport=transport)
25+
await transport.wait_for_disconnect()
26+
27+
28+
if __name__ == "__main__":
29+
uvicorn.run(app, host="0.0.0.0", port=6565)

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ gql==3.5.1
5252
websockets==15.0; python_version > "3.8"
5353
websockets==13.1; python_version == "3.8"
5454

55-
asyncwebsockets==0.9.4
55+
asyncwebsockets==0.9.4
56+
57+
fastapi==0.115.11

0 commit comments

Comments
 (0)