1+ import socketio
2+ from fastapi import FastAPI
3+
4+
5+ class SocketManager :
6+ """
7+ Creates and mounts SocketIO app to FastAPI app.
8+ Default mount location for SocketIO app is "/ws".
9+
10+ Exponses basic functionality of SocketIO.
11+
12+ Default socketio_path is "socket.io"
13+ """
14+
15+ def __init__ (
16+ self ,
17+ app : FastAPI ,
18+ mount_location : str = "/ws" ,
19+ socketio_path : str = "socket.io" ,
20+ cors_allowed_origins : list = [],
21+ ) -> None :
22+
23+ self ._sio = socketio .AsyncServer (async_mode = "asgi" , cors_allowed_origins = "*" )
24+ self ._app = socketio .ASGIApp (
25+ socketio_server = self ._sio , socketio_path = socketio_path
26+ )
27+
28+ app .mount (mount_location , self ._app )
29+ app .sio = self ._sio
30+
31+ def is_asyncio_based (self ) -> bool :
32+ return True
33+
34+ @property
35+ def on (self ):
36+ return self ._sio .on
37+
38+ @property
39+ def attach (self ):
40+ return self ._sio .attach
41+
42+ @property
43+ def emit (self ):
44+ return self ._sio .emit
45+
46+ @property
47+ def send (self ):
48+ return self ._sio .send
49+
50+ @property
51+ def call (self ):
52+ return self ._sio .call
53+
54+ @property
55+ def close_room (self ):
56+ return self ._sio .close_room
57+
58+ @property
59+ def get_session (self ):
60+ return self ._sio .get_session
61+
62+ @property
63+ def save_session (self ):
64+ return self ._sio .save_session
65+
66+ @property
67+ def session (self ):
68+ return self ._sio .session
69+
70+ @property
71+ def disconnect (self ):
72+ return self ._sio .disconnect
73+
74+ @property
75+ def handle_request (self ):
76+ return self ._sio .handle_request
77+
78+ @property
79+ def start_background_task (self ):
80+ return self ._sio .start_background_task
81+
82+ @property
83+ def sleep (self ):
84+ return self ._sio .sleep
0 commit comments