Websocket :

- Connect with token : good
    - Connect with mail/pass : good but wait for front
    - Open private msg : good but it cant send message
    - fixed some bug on printDebug on class User on wsServer
This commit is contained in:
Kum1ta
2024-08-03 23:51:09 +02:00
parent 6b40fb5927
commit 7bee4f6519
13 changed files with 270 additions and 204 deletions

View File

@ -6,10 +6,11 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/08/03 08:10:40 by edbernar #+# #+# #
# Updated: 2024/08/03 17:20:07 by edbernar ### ########.fr #
# Updated: 2024/08/03 23:13:58 by edbernar ### ########.fr #
# #
# **************************************************************************** #
from typeRequets.getPrivateListMessage import getPrivateListMessage
from typeRequets.getPrivateListUser import getPrivateListUser
from typeRequets.login import login
from Class.User import User
@ -17,10 +18,15 @@ import websockets
import asyncio
import json
connected_clients = set()
# Todo :
# - verifier que l'utilisateur n'est pas déjà connecté
# - envoyer des messages avec des caractères spéciaux type ", ', {, }, [, ]
typeRequest = ["login", "get_private_list_user"]
functionRequest = [login, getPrivateListUser]
connected_clients = []
typeRequest = ["login", "get_private_list_user", "get_private_list_message"]
functionRequest = [login, getPrivateListUser, getPrivateListMessage]
async def handler(websocket, path):
if (path != "/"):
@ -28,7 +34,7 @@ async def handler(websocket, path):
await websocket.close()
return
userClass = User(websocket)
connected_clients.add(userClass)
connected_clients.append(userClass)
try:
async for resquet in userClass.websocket:
try:
@ -37,17 +43,18 @@ async def handler(websocket, path):
await userClass.sendError("Invalid JSON", 9002)
continue
try:
userClass.printDebug(jsonRequest, 0)
if (jsonRequest["type"] in typeRequest):
if jsonRequest["type"] == "login":
await functionRequest[typeRequest.index(jsonRequest["type"])](websocket, jsonRequest["content"])
await functionRequest[typeRequest.index(jsonRequest["type"])](userClass, jsonRequest["content"])
else:
if (userClass.verifyToken(websocket, jsonRequest["token"]) == False):
if (await userClass.verifyToken(jsonRequest["token"]) == False):
continue
await functionRequest[typeRequest.index(jsonRequest["type"])](websocket)
await functionRequest[typeRequest.index(jsonRequest["type"])](userClass, jsonRequest["content"])
else:
await userClass.sendError("Invalid type", 9004)
except:
await userClass.sendError("Invalid request", 9005)
except Exception as e:
await userClass.sendError("Invalid request", 9005, e)
except websockets.ConnectionClosed:
pass
await userClass.close()