Spaces:
Sleeping
Sleeping
import flask | |
import flask_cors | |
import uuid | |
rooms = {} | |
app = flask.Flask(__name__) | |
flask_cors.CORS(app) | |
def index(): | |
return flask.render_template('index.html') | |
def create_room(): | |
roomId = flask.request.get_json()['roomIdInput'] | |
success = True | |
for room in rooms: | |
if room == roomId: | |
success = False | |
if success: | |
rooms.update({roomId: {}}) # Create an empty room with an empty message list | |
return flask.jsonify({"success": True}) | |
else: | |
return flask.jsonify({"success": False}) | |
def post(): | |
nameIn = flask.request.get_json()['nameInput'] | |
messageIn = flask.request.get_json()['messageInput'] | |
roomidIn = flask.request.get_json()['roomIdInput'] | |
n = len(rooms[roomidIn])+1 | |
rooms[roomidIn].update({"message"+str(n): {"name": nameIn, "message": messageIn}}) | |
return flask.jsonify({"success": True}) | |
def messages(): | |
print(len(rooms)) | |
roomidInput = flask.request.get_json()['roomIdInput'] | |
if roomidInput in rooms: | |
return flask.jsonify(rooms[roomidInput]) | |
else: | |
return flask.jsonify({"message": "room does not exist"}) | |
if __name__ == "__main__": | |
app.run(debug=True,host="0.0.0.0",port=5000) |