diff --git a/Twidder (Lab3)/__pycache__/database_helper.cpython-310.pyc b/Twidder (Lab3)/__pycache__/database_helper.cpython-310.pyc index 7e35810d1e1129c923a339515a845bc52bbdcae7..1e2d17a70bc4b6ffdd1d42f00a892c514cc26315 100644 Binary files a/Twidder (Lab3)/__pycache__/database_helper.cpython-310.pyc and b/Twidder (Lab3)/__pycache__/database_helper.cpython-310.pyc differ diff --git a/Twidder (Lab3)/__pycache__/server.cpython-310.pyc b/Twidder (Lab3)/__pycache__/server.cpython-310.pyc index 1489a928a291509ee805a1a5ed1b5981a0319a4d..f35c5f24951f019c99a46b894fc4bb3d7b7ab1de 100644 Binary files a/Twidder (Lab3)/__pycache__/server.cpython-310.pyc and b/Twidder (Lab3)/__pycache__/server.cpython-310.pyc differ diff --git a/Twidder (Lab3)/database.db b/Twidder (Lab3)/database.db index bd4569e38b3cb72f36be9709fc76c9f269776383..63da7616e3c379258cbc9721301d94f66fc4e410 100644 Binary files a/Twidder (Lab3)/database.db and b/Twidder (Lab3)/database.db differ diff --git a/Twidder (Lab3)/database_helper.py b/Twidder (Lab3)/database_helper.py index 877fefc84d1668faa95c898848537f2aa908a685..76a3cf7e796fe5f89d12a5203e44363e9eccbd40 100644 --- a/Twidder (Lab3)/database_helper.py +++ b/Twidder (Lab3)/database_helper.py @@ -38,25 +38,23 @@ def authenticate(email, password): 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: diff --git a/Twidder (Lab3)/server.py b/Twidder (Lab3)/server.py index e3056fa4e226e39f981a18613fc040320688a126..4a46b7fe7c53c10b981e3fef551059e882fd3079 100644 --- a/Twidder (Lab3)/server.py +++ b/Twidder (Lab3)/server.py @@ -11,23 +11,7 @@ import re app = Flask(__name__, template_folder='static') sockets = Sock(app) - -@sockets.route('/echo_connection') -def echo_connection_socket(ws): - while True: - 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") +loggedIn_for_ws = {} @sockets.route("/profil_view") def socket_connection(ws): @@ -37,7 +21,6 @@ def socket_connection(ws): if email: if email in loggedIn_for_ws: loggedIn_for_ws[email] = ws - print("loggedIn_for_ws : ", loggedIn_for_ws) else: print("ERROR : ", email, " not connected") else: @@ -87,7 +70,6 @@ def sign_in(): value = { "token" : token } - print("token : ", token) if database_helper.addToLoggedInUsers(token, data['email']): check_reconnection(data['email']) return jsonify(value), 201 @@ -263,15 +245,11 @@ def check_reconnection(email): old_ws = loggedIn_for_ws[email] old_ws.send("Disconnect") old_ws.close() - print(email, "first connection disconnected") loggedIn_for_ws[email] = -1 else: loggedIn_for_ws[email] = -1 - + if __name__ == '__main__': - # waitress.serve(app, host='0.0.0.0', port=5000, threads=4) #http://localhost:5000 - loggedIn_for_ws = {} app.debug = True app.run() - diff --git a/Twidder (Lab3)/static/client.js b/Twidder (Lab3)/static/client.js index b4e578644103d57b952f901328b8bcf595108430..a99729223490c778d496245ce06ca9db0ed5f47e 100644 --- a/Twidder (Lab3)/static/client.js +++ b/Twidder (Lab3)/static/client.js @@ -30,12 +30,10 @@ function displayProfilView() { //let ws = new WebSocket('ws://' + window.location.host + '/echo_connection') let ws = new WebSocket('ws://' + window.location.host + '/profil_view') 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"); @@ -51,9 +49,6 @@ function displayProfilView() { 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"; @@ -479,7 +474,6 @@ function display_wall(email, tab) { xhttp.send(); - } function fills_data(tab, data_user) { @@ -490,13 +484,3 @@ 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