-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (31 loc) · 879 Bytes
/
main.py
File metadata and controls
42 lines (31 loc) · 879 Bytes
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
from fastapi import FastAPI, WebSocket,WebSocketDisconnect
# from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_methods=['*'],
allow_headers=['*'],
allow_credentials=True,
)
@app.get('/')
def home():
# return RedirectResponse('/docs')
return 0
connections = []
@app.websocket('/msg')
async def message(websck : WebSocket):
await websck.accept()
# conn = websck #<== conn
connections.append(websck)
try:
while True:
data = await websck.receive_text()
except WebSocketDisconnect:
connections.remove(websck)
@app.get('/send')
async def send(msg: str):
for conn in connections:
await conn.send_text(msg)
return {"status":"ok",'message':msg}