-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
103 lines (65 loc) · 2.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from flask import Flask, render_template, request, redirect, url_for, session
from flask_socketio import SocketIO, join_room, leave_room, emit
from flask_session import Session
from encryptor import Encryptor
app = Flask(__name__)
app.debug = True
app.config["SECRET_KEY"] = "secret"
app.config["SESSION_TYPE"] = "filesystem"
listUsers = []
Session(app)
socketio = SocketIO(app, manage_session=False)
@app.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")
@app.route("/chat", methods=["GET", "POST"])
def chat():
if request.method == "POST":
username = request.form["username"]
room = request.form["room"]
encryptor = Encryptor()
encryptor.username = username
session["username"] = username
session["room"] = room
session["private_key"] = encryptor.getPrivateKey()
session["public_key"] = encryptor.getPublicKey()
listUsers.append(encryptor)
print(listUsers)
return render_template("chat.html", session=session)
else:
if session.get("username") is not None:
return render_template("chat.html", session=session)
else:
return redirect(url_for("index"))
@socketio.on("join", namespace="/chat")
def join(message):
room = session.get("room")
join_room(room)
emit("status", {"msg": session.get("username")}, room=room)
@socketio.on("text", namespace="/chat")
def text(message):
room = session.get("room")
for user in listUsers:
print(user.username)
if user.username == session.get("username"):
messageE = user.encryptMessage(message["msg"])
messageD = user.decryptMessage(messageE)
emit(
"message",
{"msg": session.get("username") + " : " + str(messageD)},
room=room,
)
emit(
"messageE",
{"msg": session.get("username") + " : " + str(messageE)},
room=room,
)
@socketio.on("left", namespace="/chat")
def left(message):
room = session.get("room")
username = session.get("username")
leave_room(room)
session.clear()
emit("status", {"msg": username + " salió"}, room=room)
if __name__ == "__main__":
socketio.run(app)