From fcee64600bf2a97d3af0e8491d3ddd14de6a823a Mon Sep 17 00:00:00 2001 From: ComeF2 <123363885+ComeF2@users.noreply.github.com> Date: Sun, 26 Feb 2023 21:28:21 +0100 Subject: [PATCH] =?UTF-8?q?Premi=C3=A8re=20impl=C3=A9mentation=20r=C3=A9us?= =?UTF-8?q?si=20step=203=20Lab=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../database_helper.cpython-310.pyc | Bin 3746 -> 4076 bytes Twidder (Lab3)/database.db | Bin 36864 -> 36864 bytes Twidder (Lab3)/database_helper.py | 25 ++++++++++- Twidder (Lab3)/server.py | 32 ++++++++++---- Twidder (Lab3)/static/client.html | 1 + Twidder (Lab3)/static/client.js | 41 +++++++++++++++--- 6 files changed, 84 insertions(+), 15 deletions(-) diff --git a/Twidder (Lab3)/__pycache__/database_helper.cpython-310.pyc b/Twidder (Lab3)/__pycache__/database_helper.cpython-310.pyc index a589a1c66f72efe21dc63cfcf42da73d2da85dd5..7e35810d1e1129c923a339515a845bc52bbdcae7 100644 GIT binary patch delta 950 zcmZ1^`$nEGpO=@5fq{YHnb-GZ4&jM>GK_r_wT+oL88*h<Wv*vsU|?WlU|?`&U|=Y& zVqjpXGAm(dW~gPVVOqerkfD~jhIs)~3S$jp8skDHMur;Z1<VT>YFTPnQW#1Zid0Hi zQka?<!QxCQ%ry)(jA=|Db$L7`EGaB-8CHZ$4buYFg$#@g;S4Ek!3=_o3?LZEkis6! zpvmD^r8&8XRjJ<9*U{6**1k%^EwiY&M4==zH&vmaw4@|6FI}OuIJHP2Gf%<C-`(BS z#nUe|*fl7)%FNR**fl6b!P76qA5}_0BgEg^)lWwOY^bI}n4?dqYp{Zby^eytW|d}e zYI1&F3aS<L*zM2@4RCP`aYZve*fm4}Y=f=6Lb!)(kSoL@Tl)|<O|~LX&=-j_FfbJH zfCvc&28LU#1x1;8B}Kdp3=EpAw^&Q^vs3em_(0OkAVQphi-F-5TTyCpX->)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T@+97D zs4aqg!V;|@wHyo#3=ng87>hI+7#Nb7C-ZShOb+3@D&7N90J4aQQ3MfZx|1FFwWVZ0 z(IE#S6hQ<idW)1nEY-;k{1S}1ljrfPsewc}7<d?2m^hd?*f}^kcsMvY_&9huxH)(@ NcsV#ZI5@a?*a7@8a{mAT diff --git a/Twidder (Lab3)/database.db b/Twidder (Lab3)/database.db index 4213d44eb0fac01c7a52ea7879431312ace92b86..e782f056d664cc752ac1faf34ef066c94f2b9145 100644 GIT binary patch delta 138 zcmZozz|^pSX@V3J6XQe~CnhGwjR{HqDv24MX8C5$5gBHgZr;u=#_2vLmg#AJzRs@M zZWYNUZpmRK#hah`^D2lKG9Uq71_lO0j`@?>3*uQgPH@cM*l5bZu5QT6ps6W2S-w(+ dH>J3&I6X<t03;#~5@Gd7tyECl{GmcX901RaBUJzZ delta 97 zcmZozz|^pSX@V3J<D7{yPE3q*HYOzbs}#AU<pvlU<Q4fOyJz@T<fVj1W;s_GrKDII s<oJ2!NBNf)mTi9K&#NG!&wvDY85kJ!IX+BgFNojR*u}B=Lxq4i0L%g$y#N3J diff --git a/Twidder (Lab3)/database_helper.py b/Twidder (Lab3)/database_helper.py index 7eac6bd..877fefc 100644 --- a/Twidder (Lab3)/database_helper.py +++ b/Twidder (Lab3)/database_helper.py @@ -31,11 +31,34 @@ def authenticate(email, password): return False -def addToLoggedInUsers(token, email): +"""def addToLoggedInUsers(token, email): try: get_db().execute("INSERT into LOGGEDINUSERS values(?, ?)", [token, email]) get_db().commit() return True + except: + return False""" + +def addToLoggedInUsers(token, email): + try: + db = get_db() + cursor = db.cursor() + + # Check if the email already exists in the table + cursor.execute("SELECT * FROM LOGGEDINUSERS WHERE EMAIL=?", (email,)) + result = cursor.fetchone() + + if result is None: + print("First time putting user in LOGGEDINUSERS") + # If the email is not already in the table, insert a new row + cursor.execute("INSERT INTO LOGGEDINUSERS (TOKEN, EMAIL) VALUES (?, ?)", (token, email)) + else: + print("Second time putting user in LOGGEDINUSERS") + # If the email already exists in the table, update the corresponding token + cursor.execute("UPDATE LOGGEDINUSERS SET TOKEN=? WHERE EMAIL=?", (token, email)) + + db.commit() + return True except: return False diff --git a/Twidder (Lab3)/server.py b/Twidder (Lab3)/server.py index 3e61cae..8b4cafc 100644 --- a/Twidder (Lab3)/server.py +++ b/Twidder (Lab3)/server.py @@ -1,5 +1,5 @@ from flask import Flask, request, jsonify -from flask_sockets import Sockets +from flask_sock import Sock import waitress import database_helper @@ -8,15 +8,27 @@ import string import json import re -app = Flask(__name__) -sockets = Sockets(app) +app = Flask(__name__, template_folder='static') +sockets = Sock(app) -@sockets.route('/echo') -def echo_socket(ws): + +@sockets.route('/echo_connection') +def echo_connection_socket(ws): while True: - message = ws.receive() - print("message : ", message) - # ws.send(message) + token = ws.receive() + email = database_helper.tokenToEmail(token) + if email: + if email in loggedIn_for_ws: + old_ws = loggedIn_for_ws[email] + old_ws.send("Disconnect") + old_ws.close() + print(email, "first connection disconnected") + loggedIn_for_ws[email] = ws + else: + loggedIn_for_ws[email] = ws + else: + print("email error") + @app.route("/", methods = ['GET']) def hello_world(): @@ -62,6 +74,7 @@ def sign_in(): value = { "token" : token } + print("token : ", token) if database_helper.addToLoggedInUsers(token, data['email']): return jsonify(value), 201 else: @@ -233,7 +246,8 @@ def is_valid_email(email): if __name__ == '__main__': + # waitress.serve(app, host='0.0.0.0', port=5000, threads=4) #http://localhost:5000 + loggedIn_for_ws = {} app.debug = True - #waitress.serve(app, host='0.0.0.0', port=5000, threads=4) #http://localhost:5000 app.run() diff --git a/Twidder (Lab3)/static/client.html b/Twidder (Lab3)/static/client.html index 6ef54ed..21b2419 100644 --- a/Twidder (Lab3)/static/client.html +++ b/Twidder (Lab3)/static/client.html @@ -6,6 +6,7 @@ <link href="/static/client.css" type="text/css" rel="stylesheet"> <script src="/static/client.js"></script> <script src="/static/serverstub.js"></script> + <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.0/socket.io.js"></script> <script id="welcomeview" type="text/view"> <div id="center"> <img src="/static/wimage.png"> diff --git a/Twidder (Lab3)/static/client.js b/Twidder (Lab3)/static/client.js index 180f6a4..8999723 100644 --- a/Twidder (Lab3)/static/client.js +++ b/Twidder (Lab3)/static/client.js @@ -23,15 +23,36 @@ window.onload = function() { //---------------------------------------WELCOME & PROFIL--------------------------------------- function displayWelcomeView() { - var ws = new WebSocket("ws://localhost:5000/echo") - ws.onopen = function(event) { - console.log("Connexion WebSocket établie !"); - ws.send("Hello, server!"); - }; document.getElementById("displaywelcome").innerHTML = document.getElementById("welcomeview").innerHTML; } function displayProfilView() { + let ws = new WebSocket('ws://' + window.location.host + '/echo_connection') + ws.onopen = function(event) { + console.log("WebSocket connection established !"); + ws.send(localStorage.getItem("token")); + }; + ws.onmessage = (event) => { + if (event.data == "Disconnect"){ + console.log("Disconnection") + displayWelcomeView(); + localStorage.removeItem("tab"); + localStorage.removeItem("token"); + localStorage.removeItem("browsed_email"); + localStorage.removeItem("email_username"); + localStorage.removeItem("refreshToken"); + localStorage.removeItem("refreshToken2"); + localStorage.removeItem("reloadHome"); + localStorage.removeItem("reloadBrowsed"); + localStorage.removeItem("browse_success"); + document.getElementById("displayprofil").innerHTML = ""; + document.getElementById("displaywelcome").innerHTML = document.getElementById("welcomeview").innerHTML; + document.body.style.backgroundColor = "#0F98B7"; + } + } + ws.onclose = function() { + console.log('WebSocket disconnected'); + }; document.getElementById("displayprofil").innerHTML = document.getElementById("profilview").innerHTML; document.getElementById("displaywelcome").innerHTML = ""; document.body.style.backgroundColor = "#fcd29f"; @@ -468,3 +489,13 @@ function fills_data(tab, data_user) { document.getElementById(tab + "_city").innerHTML = "City = " + data_user.data.city; document.getElementById(tab + "_gender").innerHTML = "Gender = " + data_user.data.gender; } + + + +//---------------------------------------WEBSOCKET--------------------------------------- + +/*let ws = new WebSocket('ws://' + window.location.host + '/echo_connection') + +ws.onmessage = (event) => { + console.log("event.data : ", event.data); +}*/ \ No newline at end of file -- GitLab