diff --git a/app.py b/app.py
index aaadef8..8b61b5e 100644
--- a/app.py
+++ b/app.py
@@ -9,10 +9,13 @@ from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import time
import re
+from flask_socketio import SocketIO, emit
+
dotenv.load_dotenv()
app = Flask(__name__)
+socketio = SocketIO(app)
app.secret_key = os.getenv("SECRET")
USERS_FILE = "users.txt"
DATA_DIR = "notes"
@@ -127,6 +130,7 @@ def public_board(username):
return f.read(), 200
if request.method == "POST":
+
if user != username:
return "Forbidden", 403
data = request.get_json()
@@ -135,24 +139,34 @@ def public_board(username):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(data["content"])
+ socketio.emit('board_update', {'content': data["content"]})
return jsonify({"status": "saved"})
+@socketio.on('connect')
+def handle_connect():
+ print('Client connected')
+
+@socketio.on('disconnect')
+def handle_disconnect():
+ print('Client disconnected')
+
+# When content is updated via POST /boardapi
@app.route("/boardapi", methods=["GET", "POST"])
def shared_board():
path = os.path.join(DATA_DIR, "__shared_board.txt")
-
if request.method == "GET":
if not os.path.exists(path):
return "", 200
with open(path, "r", encoding="utf-8") as f:
return f.read(), 200
-
- if request.method == "POST":
+ elif request.method == "POST":
data = request.get_json()
if not data or "content" not in data:
return "Bad request", 400
with open(path, "w", encoding="utf-8") as f:
f.write(data["content"])
+ # Broadcast new content to all clients
+ socketio.emit('board_update', {'content': data["content"]})
return jsonify({"status": "saved"})
diff --git a/static/note.js b/static/note.js
index f2aa128..794392b 100644
--- a/static/note.js
+++ b/static/note.js
@@ -79,3 +79,15 @@ textarea.addEventListener("input", () => {
}
}, 500);
});
+
+
+const socket = io();
+
+// Listen for updates
+socket.on('board_update', data => {
+ if (true && data.content !== last) {
+ textarea.value = data.content;
+ last = data.content;
+ status.textContent = "Updated from server";
+ }
+});
\ No newline at end of file
diff --git a/templates/note.html b/templates/note.html
index 281e5c9..6b41c13 100644
--- a/templates/note.html
+++ b/templates/note.html
@@ -16,7 +16,9 @@
+