Skip to content

Commit 75cf41a

Browse files
committed
Add asynchronous socketio application server
1 parent a3d079c commit 75cf41a

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

backend/app/task/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 任务介绍
22

3-
当前任务使用 Celery 实现,实施方案请查看 [#225](https://github.com/orgs/fastapi-practices/discussions/225)
3+
当前任务使用 Celery
4+
实现,实施方案请查看 [#225](https://github.com/fastapi-practices/fastapi_best_architecture/discussions/225)
45

56
## 添加任务
67

backend/common/socket.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import socketio
4+
5+
from backend.common.log import log
6+
from backend.core.conf import settings
7+
8+
sio = socketio.AsyncServer(
9+
async_mode='asgi',
10+
cors_credentials=True,
11+
cors_allowed_origins=settings.CORS_ALLOWED_ORIGINS,
12+
namespaces=['/ws'],
13+
logger=True,
14+
engineio_logger=True
15+
)
16+
17+
18+
@sio.event
19+
async def connect(sid, environ):
20+
log.debug(sid, environ)
21+
await sio.emit('message', {'data': 'Welcome!'}, to=sid)
22+
23+
24+
@sio.event
25+
async def disconnect(sid):
26+
log.debug(sid)

backend/core/registrar.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# -*- coding: utf-8 -*-
33
from contextlib import asynccontextmanager
44

5+
import socketio
6+
57
from asgi_correlation_id import CorrelationIdMiddleware
68
from fastapi import Depends, FastAPI
79
from fastapi_limiter import FastAPILimiter
@@ -11,6 +13,7 @@
1113
from backend.app.router import route
1214
from backend.common.exception.exception_handler import register_exception
1315
from backend.common.log import set_customize_logfile, setup_logging
16+
from backend.common.socket import sio
1417
from backend.core.conf import settings
1518
from backend.core.path_conf import STATIC_DIR
1619
from backend.database.db_mysql import create_table
@@ -79,6 +82,9 @@ def register_app():
7982
# 全局异常处理
8083
register_exception(app)
8184

85+
# socket 应用
86+
register_socket_app(app)
87+
8288
return app
8389

8490

@@ -106,6 +112,7 @@ def register_static_file(app: FastAPI):
106112

107113
if not os.path.exists(STATIC_DIR):
108114
os.mkdir(STATIC_DIR)
115+
109116
app.mount('/static', StaticFiles(directory=STATIC_DIR), name='static')
110117

111118

@@ -170,3 +177,14 @@ def register_page(app: FastAPI):
170177
:return:
171178
"""
172179
add_pagination(app)
180+
181+
182+
def register_socket_app(app: FastAPI):
183+
"""
184+
socketio
185+
186+
:param app:
187+
:return:
188+
"""
189+
socket_app = socketio.ASGIApp(sio, app)
190+
app.mount('/ws', socket_app)

backend/pdm.lock

Lines changed: 84 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ dependencies = [
5050
# https://github.com/celery/celery/issues/7874
5151
"celery-aio-pool==0.1.0rc6",
5252
"asgi-correlation-id>=4.3.3",
53+
"python-socketio[asyncio]>=5.11.4",
5354
]
5455
requires-python = ">=3.10,<3.13"
5556
readme = "README.md"

backend/requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ async-timeout==4.0.3; python_full_version < "3.11.3"
1212
asyncmy==0.2.9
1313
attrs==24.2.0
1414
bcrypt==4.0.1
15+
bidict==0.23.1
1516
billiard==4.2.0
1617
casbin==1.34.0
1718
casbin-async-sqlalchemy-adapter==1.4.0
@@ -84,8 +85,11 @@ pytest==7.2.2
8485
pytest-pretty==1.2.0
8586
python-dateutil==2.9.0.post0
8687
python-dotenv==1.0.1
88+
python-engineio==4.9.1
8789
python-jose==3.3.0
8890
python-multipart==0.0.9
91+
python-socketio==5.11.4
92+
python-socketio[asyncio]==5.11.4
8993
pytz==2024.1
9094
pyyaml==6.0.2
9195
redis==5.1.0
@@ -95,6 +99,7 @@ rsa==4.9
9599
ruff==0.6.4
96100
setuptools==74.1.2
97101
shellingham==1.5.4
102+
simple-websocket==1.0.0
98103
simpleeval==0.9.13
99104
six==1.16.0
100105
sniffio==1.3.1
@@ -120,4 +125,5 @@ watchfiles==0.24.0
120125
wcwidth==0.2.13
121126
websockets==13.0.1
122127
win32-setctime==1.1.0; sys_platform == "win32"
128+
wsproto==1.2.0
123129
xdbsearchip==1.0.2

0 commit comments

Comments
 (0)