Skip to content
Snippets Groups Projects
Commit 6f544485 authored by ComeF2's avatar ComeF2
Browse files

Fini step 3

parent fcee6460
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -29,6 +29,19 @@ def echo_connection_socket(ws):
else:
print("email error")
@sockets.route("/profil_view")
def socket_connection(ws):
while True:
token = ws.receive()
email = database_helper.tokenToEmail(token)
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:
print("ERROR : ", email, " not found")
@app.route("/", methods = ['GET'])
def hello_world():
......@@ -76,6 +89,7 @@ def sign_in():
}
print("token : ", token)
if database_helper.addToLoggedInUsers(token, data['email']):
check_reconnection(data['email'])
return jsonify(value), 201
else:
return "", 409
......@@ -244,6 +258,16 @@ def is_valid_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
def check_reconnection(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] = -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
......
......@@ -27,7 +27,8 @@ function displayWelcomeView() {
}
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')
ws.onopen = function(event) {
console.log("WebSocket connection established !");
ws.send(localStorage.getItem("token"));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment