This repository was archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathsocket_manager.py
More file actions
45 lines (36 loc) · 1.48 KB
/
Copy pathsocket_manager.py
File metadata and controls
45 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import Union
import socketio
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
class SocketManager(socketio.AsyncServer):
"""
Integrates SocketIO with FastAPI app.
Adds `sio` property to FastAPI object (app).
Default mount location for SocketIO app is at `/ws`
and defautl SocketIO path is `socket.io`.
(e.g. full path: `ws://www.example.com/ws/socket.io/)
SocketManager exposes basic underlying SocketIO functionality
e.g. emit, on, send, call, etc.
"""
def __init__(
self,
app: FastAPI,
mount_location: str = "/ws",
socketio_path: str = "socket.io",
cors_allowed_origins: Union[str, list] = '*',
async_mode: str = "asgi",
**kwargs
) -> None:
middleware = next((x for x in app.user_middleware if issubclass(x.cls, CORSMiddleware)), None)
if middleware:
cors_allowed_origins = middleware.options.get("allow_origins", "*")
super().__init__(cors_allowed_origins=cors_allowed_origins, async_mode=async_mode, **kwargs)
self._app = socketio.ASGIApp(
socketio_server=self, socketio_path=socketio_path
)
app.mount(mount_location, self._app)
app.add_route(f"/{socketio_path}/", route=self._app, methods=["GET", "POST"])
app.add_websocket_route(f"/{socketio_path}/", self._app)
app.sio = self
def is_asyncio_based(self) -> bool:
return True