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,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/08/03 15:54:14 by edbernar #+# #+# #
# Updated: 2024/08/03 17:17:39 by edbernar ### ########.fr #
# Updated: 2024/08/03 23:30:42 by edbernar ### ########.fr #
# #
# **************************************************************************** #
@ -15,7 +15,7 @@ import asyncio
import json
class User(websockets.WebSocketServerProtocol):
class User():
debugMode = True
websocket = None
username = ""
@ -24,56 +24,92 @@ class User(websockets.WebSocketServerProtocol):
def __init__(self, websocket):
if (self.debugMode):
print("\033[0;34m|------ New user Connected ------|\033[0;0m")
print("\033[42m|------ New user Connected ------|\033[1;0m")
self.websocket = websocket
def __del__(self):
if (self.debugMode):
print("\033[0;31m|------ User disconnected ------|\033[0;0m")
print("\033[43m|------ User disconnected -------|\033[1;0m")
print("User :", self.username)
print("Id :", self.id)
async def sendError(self, message, code, error=None):
try:
jsonVar = {"type": "error", "content": message, "code": code}
self.printDebug(jsonVar, 2, error)
await self.websocket.send(json.dumps(jsonVar))
except:
if (self.debugMode):
print("\033[0;31m|------ Error in sendError ------|\033[0;0m")
async def sendError(self, message, code):
jsonVar = {"type": "error", "content": message, "code": code}
self.printDebug( jsonVar, 2)
await self.websocket.send(json.dumps(jsonVar))
async def send(self, content):
self.printDebug(content, 1)
if (type(content) == dict):
await self.websocket.send(json.dumps(content))
else:
await self.websocket.send(content)
try:
if (type(content) == dict):
self.printDebug(content, 1)
await self.websocket.send(json.dumps(content))
else:
self.printDebug(json.loads(content), 1)
await self.websocket.send(content)
except Exception as e:
if (self.debugMode):
print("\033[0;31m|--------- Error in send --------|\033[0;0m")
print("Error message :", e)
async def verifyToken(self, token):
if (self.token != token or self.token == ""):
await self.sendError("Invalid token", 9001)
return False
return True
def printDebug(self, infoUser, request, typeRequest):
try:
if (self.token != token or self.token == ""):
await self.sendError("Invalid token", 9001)
return False
return True
except:
if (self.debugMode):
print("\033[0;31m|----- Error in verifyToken -----|\033[0;0m")
def printDebug(self, request, typeRequest, error=None):
try:
if (self.debugMode and typeRequest == 0):
print("\033[0;34m|----- New received request -----|\033[0;0m")
print("User :", infoUser.username)
print("Token :", infoUser.token)
print("Id :", infoUser.id)
print("Var type :", type(request["type"]))
print("Type :", request["type"])
print("Content :", request["content"])
print("User :", self.username)
print("Token :", self.token)
print("Id :", self.id)
try:
print("Var type :", type(request["type"]))
print("Type :", request["type"])
except:
pass
try:
print("Content type :", type(request["content"]))
except:
pass
elif (self.debugMode and typeRequest == 1):
print("\033[0;32m|------- New sent request -------|\033[0;0m")
print("To :", infoUser.username)
print("Var type :", type(request["type"]))
print("Type :", request["type"])
print("Content :", request["content"])
print("To :", self.username)
print("Id :", self.id)
try:
print("Var type :", type(request["type"]))
print("Type :", request["type"])
except:
pass
try:
print("Content :", request["content"])
except:
pass
if ("type" not in request or "content" not in request):
print("Warning : not usual json format")
elif (self.debugMode and typeRequest == 2):
print("\033[0;31m|------------- Error ------------|\033[0;0m")
print("User :", infoUser.username)
print("Token :", infoUser.token)
print("Id :", infoUser.id)
print("User :", self.username)
print("Token :", self.token)
print("Id :", self.id)
print("Error message :", request["content"])
print("Error code :", request["code"])
if (error != None):
print("Error python :", error)
print("File :", error.__traceback__.tb_frame.f_globals["__file__"])
print("Line :", error.__traceback__.tb_lineno)
except:
print("\033[0;31m|------- Error in printDebug -----|\033[0;0m")
print("\033[0;31m|------ Error in printDebug -----|\033[0;0m")
async def close(self):
try: