Изменение вебсокета
This commit is contained in:
parent
cf76cccc66
commit
2ac67a9e61
2 changed files with 22 additions and 8 deletions
|
@ -1,3 +1,4 @@
|
|||
import websockets
|
||||
from fastapi import WebSocket, WebSocketDisconnect, Depends
|
||||
|
||||
from app.exceptions import IncorrectDataException, UserDontHavePermissionException
|
||||
|
@ -6,7 +7,7 @@ from app.unit_of_work import UnitOfWork
|
|||
from app.utils.auth import AuthService
|
||||
from app.chat.router import router
|
||||
from app.chat.shemas import SSendMessage, SMessage, SDeleteMessage, SEditMessage, SPinMessage, SUnpinMessage
|
||||
from app.users.dependencies import get_current_user_ws
|
||||
from app.users.dependencies import get_current_user_ws, get_token
|
||||
from app.users.schemas import SUser
|
||||
|
||||
|
||||
|
@ -130,14 +131,15 @@ manager = ConnectionManager()
|
|||
"/ws/{chat_id}",
|
||||
)
|
||||
async def websocket_endpoint(
|
||||
chat_id: int,
|
||||
websocket: WebSocket,
|
||||
user: SUser = Depends(get_current_user_ws),
|
||||
uow=Depends(UnitOfWork)
|
||||
chat_id: int,
|
||||
websocket: WebSocket,
|
||||
user: SUser = Depends(get_current_user_ws),
|
||||
uow=Depends(UnitOfWork)
|
||||
):
|
||||
await AuthService.check_verificated_user_with_exc(uow=uow, user_id=user.id)
|
||||
await AuthService.validate_user_access_to_chat(uow=uow, user_id=user.id, chat_id=chat_id)
|
||||
await manager.connect(chat_id, websocket)
|
||||
await websocket.send_json({"scope": str(websocket.scope), "cookies": str(websocket.cookies), "headers": str(websocket.headers)})
|
||||
try:
|
||||
while True:
|
||||
data = await websocket.receive_json()
|
||||
|
@ -145,3 +147,15 @@ async def websocket_endpoint(
|
|||
await manager.broadcast(uow=uow, user_id=user.id, chat_id=chat_id, message=data)
|
||||
except WebSocketDisconnect:
|
||||
manager.disconnect(chat_id, websocket)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/ws/{chat_id}"
|
||||
)
|
||||
async def websocket_endpoint(
|
||||
chat_id: int,
|
||||
token: str = Depends(get_token),
|
||||
):
|
||||
url = f"ws://localhost:8000/api/chat/ws/{chat_id}"
|
||||
async with websockets.connect(url, extra_headers={"Authorization": f"Bearer {token}"}) as websocket:
|
||||
print(await websocket.recv())
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, Header
|
||||
from fastapi import Depends, Header, WebSocket
|
||||
from fastapi.security import HTTPBearer
|
||||
from jose import JWTError, jwt, ExpiredSignatureError
|
||||
|
||||
|
@ -50,8 +50,8 @@ async def check_verificated_user_with_exc(user: SUser = Depends(get_current_user
|
|||
return user
|
||||
|
||||
|
||||
def get_token_ws(authorization: Annotated[str | None, Header()] = None) -> str:
|
||||
if authorization in None:
|
||||
def get_token_ws(websocket: WebSocket, authorization: Annotated[str | None, Header()] = None) -> str:
|
||||
if authorization is None:
|
||||
raise TokenAbsentException
|
||||
return authorization.split()[1]
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue