-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
35 lines (27 loc) · 938 Bytes
/
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
from flask import Flask
from flask_cors import CORS
from flask_socketio import SocketIO, join_room
app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="https://stephenfujiwara-chat-client.netlify.app")
@app.route("/")
def home():
return "Home route"
@app.route("/chat")
def chat():
return "Chat route"
@socketio.on("connect", namespace="/chat")
def handle_connection():
print("someone has connected")
@socketio.on("disconnect", namespace="/chat")
def handle_disconnect():
print("someone has disconnected")
@socketio.on("join_room", namespace="/chat")
def handle_join_room(data):
join_room(data["room"])
print(f"User {data['id']} has joined room {data['room']}")
@socketio.on("send_message", namespace="/chat")
def handle_send_message(data):
socketio.emit("receive_message", data, namespace="/chat", to=data["room"])
if __name__ == "__main__":
socketio.run(app, debug=True)