File size: 966 Bytes
fbae503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_socketio import SocketIO, join_room, emit

# Initialize the Flask app and SocketIO instance
app = Flask(__name__)
socketio = SocketIO(app)

# Handle the connection event
@socketio.on('connect')
def handle_connect():
    print("User connected")

# Handle room joining
@socketio.on('joinRoom')
def handle_join_room(room_id):
    join_room(room_id)
    print(f"User joined room: {room_id}")

# Handle receiving and broadcasting chat messages
@socketio.on('chatMessage')
def handle_chat_message(msg, room_id):
    print(f"Message received: {msg}")
    # Broadcast the message to everyone in the room
    emit('chatMessage', msg, room=room_id)

# Handle user disconnection
@socketio.on('disconnect')
def handle_disconnect():
    print("User disconnected")

# Start the server when running this script
if __name__ == '__main__':
    # Run the server on the Hugging Face Space environment
    socketio.run(app, host='0.0.0.0', port=5000)