Ashraf-CK commited on
Commit
fbae503
·
verified ·
1 Parent(s): 1f86f16

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask
2
+ from flask_socketio import SocketIO, join_room, emit
3
+
4
+ # Initialize the Flask app and SocketIO instance
5
+ app = Flask(__name__)
6
+ socketio = SocketIO(app)
7
+
8
+ # Handle the connection event
9
+ @socketio.on('connect')
10
+ def handle_connect():
11
+ print("User connected")
12
+
13
+ # Handle room joining
14
+ @socketio.on('joinRoom')
15
+ def handle_join_room(room_id):
16
+ join_room(room_id)
17
+ print(f"User joined room: {room_id}")
18
+
19
+ # Handle receiving and broadcasting chat messages
20
+ @socketio.on('chatMessage')
21
+ def handle_chat_message(msg, room_id):
22
+ print(f"Message received: {msg}")
23
+ # Broadcast the message to everyone in the room
24
+ emit('chatMessage', msg, room=room_id)
25
+
26
+ # Handle user disconnection
27
+ @socketio.on('disconnect')
28
+ def handle_disconnect():
29
+ print("User disconnected")
30
+
31
+ # Start the server when running this script
32
+ if __name__ == '__main__':
33
+ # Run the server on the Hugging Face Space environment
34
+ socketio.run(app, host='0.0.0.0', port=5000)