Skip to content
Snippets Groups Projects
Commit cf390e97 authored by MaximeOLIVA's avatar MaximeOLIVA
Browse files

modif lab3

parent 6f544485
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -38,25 +38,23 @@ def authenticate(email, password): ...@@ -38,25 +38,23 @@ def authenticate(email, password):
return True return True
except: except:
return False""" return False"""
def addToLoggedInUsers(token, email): def addToLoggedInUsers(token, email):
try: try:
db = get_db() db = get_db()
cursor = db.cursor() cursor = db.cursor()
# Check if the email already exists in the table # Check if the email already exists in the table
cursor.execute("SELECT * FROM LOGGEDINUSERS WHERE EMAIL=?", (email,)) cursor.execute("SELECT * FROM LOGGEDINUSERS WHERE EMAIL=?", (email,))
result = cursor.fetchone() result = cursor.fetchone()
if result is None: if result is None:
print("First time putting user in LOGGEDINUSERS")
# If the email is not already in the table, insert a new row # If the email is not already in the table, insert a new row
cursor.execute("INSERT INTO LOGGEDINUSERS (TOKEN, EMAIL) VALUES (?, ?)", (token, email)) cursor.execute("INSERT INTO LOGGEDINUSERS (TOKEN, EMAIL) VALUES (?, ?)", (token, email))
else: else:
print("Second time putting user in LOGGEDINUSERS")
# If the email already exists in the table, update the corresponding token # If the email already exists in the table, update the corresponding token
cursor.execute("UPDATE LOGGEDINUSERS SET TOKEN=? WHERE EMAIL=?", (token, email)) cursor.execute("UPDATE LOGGEDINUSERS SET TOKEN=? WHERE EMAIL=?", (token, email))
db.commit() db.commit()
return True return True
except: except:
......
...@@ -11,23 +11,7 @@ import re ...@@ -11,23 +11,7 @@ import re
app = Flask(__name__, template_folder='static') app = Flask(__name__, template_folder='static')
sockets = Sock(app) sockets = Sock(app)
loggedIn_for_ws = {}
@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")
@sockets.route("/profil_view") @sockets.route("/profil_view")
def socket_connection(ws): def socket_connection(ws):
...@@ -37,7 +21,6 @@ def socket_connection(ws): ...@@ -37,7 +21,6 @@ def socket_connection(ws):
if email: if email:
if email in loggedIn_for_ws: if email in loggedIn_for_ws:
loggedIn_for_ws[email] = ws loggedIn_for_ws[email] = ws
print("loggedIn_for_ws : ", loggedIn_for_ws)
else: else:
print("ERROR : ", email, " not connected") print("ERROR : ", email, " not connected")
else: else:
...@@ -87,7 +70,6 @@ def sign_in(): ...@@ -87,7 +70,6 @@ def sign_in():
value = { value = {
"token" : token "token" : token
} }
print("token : ", token)
if database_helper.addToLoggedInUsers(token, data['email']): if database_helper.addToLoggedInUsers(token, data['email']):
check_reconnection(data['email']) check_reconnection(data['email'])
return jsonify(value), 201 return jsonify(value), 201
...@@ -263,15 +245,11 @@ def check_reconnection(email): ...@@ -263,15 +245,11 @@ def check_reconnection(email):
old_ws = loggedIn_for_ws[email] old_ws = loggedIn_for_ws[email]
old_ws.send("Disconnect") old_ws.send("Disconnect")
old_ws.close() old_ws.close()
print(email, "first connection disconnected")
loggedIn_for_ws[email] = -1 loggedIn_for_ws[email] = -1
else: else:
loggedIn_for_ws[email] = -1 loggedIn_for_ws[email] = -1
if __name__ == '__main__': 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.debug = True
app.run() app.run()
...@@ -30,12 +30,10 @@ function displayProfilView() { ...@@ -30,12 +30,10 @@ function displayProfilView() {
//let ws = new WebSocket('ws://' + window.location.host + '/echo_connection') //let ws = new WebSocket('ws://' + window.location.host + '/echo_connection')
let ws = new WebSocket('ws://' + window.location.host + '/profil_view') let ws = new WebSocket('ws://' + window.location.host + '/profil_view')
ws.onopen = function(event) { ws.onopen = function(event) {
console.log("WebSocket connection established !");
ws.send(localStorage.getItem("token")); ws.send(localStorage.getItem("token"));
}; };
ws.onmessage = (event) => { ws.onmessage = (event) => {
if (event.data == "Disconnect"){ if (event.data == "Disconnect"){
console.log("Disconnection")
displayWelcomeView(); displayWelcomeView();
localStorage.removeItem("tab"); localStorage.removeItem("tab");
localStorage.removeItem("token"); localStorage.removeItem("token");
...@@ -51,9 +49,6 @@ function displayProfilView() { ...@@ -51,9 +49,6 @@ function displayProfilView() {
document.body.style.backgroundColor = "#0F98B7"; document.body.style.backgroundColor = "#0F98B7";
} }
} }
ws.onclose = function() {
console.log('WebSocket disconnected');
};
document.getElementById("displayprofil").innerHTML = document.getElementById("profilview").innerHTML; document.getElementById("displayprofil").innerHTML = document.getElementById("profilview").innerHTML;
document.getElementById("displaywelcome").innerHTML = ""; document.getElementById("displaywelcome").innerHTML = "";
document.body.style.backgroundColor = "#fcd29f"; document.body.style.backgroundColor = "#fcd29f";
...@@ -479,7 +474,6 @@ function display_wall(email, tab) { ...@@ -479,7 +474,6 @@ function display_wall(email, tab) {
xhttp.send(); xhttp.send();
} }
function fills_data(tab, data_user) { function fills_data(tab, data_user) {
...@@ -490,13 +484,3 @@ 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 + "_city").innerHTML = "City = " + data_user.data.city;
document.getElementById(tab + "_gender").innerHTML = "Gender = " + data_user.data.gender; 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment