UPDATE: Improved chat transmission speed by ~50%
This commit is contained in:
@@ -22,7 +22,7 @@ services:
|
|||||||
- .env
|
- .env
|
||||||
environment:
|
environment:
|
||||||
- FLASK_APP=blueprints.__init__
|
- FLASK_APP=blueprints.__init__
|
||||||
- FLASK_DEBUG=True
|
- FLASK_ENV=production
|
||||||
|
|
||||||
frontend:
|
frontend:
|
||||||
build:
|
build:
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ const StreamerRoute: React.FC = () => {
|
|||||||
checkStreamStatus();
|
checkStreamStatus();
|
||||||
|
|
||||||
// Poll for live status changes
|
// Poll for live status changes
|
||||||
const interval = setInterval(checkStreamStatus, 1000); // Check every 90 seconds
|
const interval = setInterval(checkStreamStatus, 30000); // Check every 90 seconds
|
||||||
|
|
||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, [streamerName]);
|
}, [streamerName]);
|
||||||
|
|||||||
@@ -2,20 +2,19 @@ from flask import Blueprint, jsonify, session
|
|||||||
from database.database import Database
|
from database.database import Database
|
||||||
from flask_socketio import SocketIO, emit, join_room, leave_room
|
from flask_socketio import SocketIO, emit, join_room, leave_room
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from flask_socketio import SocketIO
|
||||||
|
|
||||||
chat_bp = Blueprint("chat", __name__)
|
chat_bp = Blueprint("chat", __name__)
|
||||||
socketio = SocketIO()
|
socketio = SocketIO(cors_allowed_origins="*")
|
||||||
|
|
||||||
# <---------------------- ROUTES NEEDS TO BE CHANGED TO VIDEO OR DELETED AS DEEMED APPROPRIATE ---------------------->
|
# <---------------------- ROUTES NEEDS TO BE CHANGED TO VIDEO OR DELETED AS DEEMED APPROPRIATE ---------------------->
|
||||||
# TODO: Add a route that deletes all chat logs when the stream is finished
|
|
||||||
|
|
||||||
|
|
||||||
@socketio.on("connect")
|
@socketio.on("connect")
|
||||||
def handle_connection() -> None:
|
def handle_connection() -> None:
|
||||||
"""
|
"""
|
||||||
Accept the connection from the frontend.
|
Accept the connection from the frontend.
|
||||||
"""
|
"""
|
||||||
print("Client Connected") # Confirmation connect has been made
|
print("Client Connected") # Confirmation connect has been made
|
||||||
|
|
||||||
|
|
||||||
@socketio.on("join")
|
@socketio.on("join")
|
||||||
@@ -62,14 +61,13 @@ def get_past_chat(stream_id: int):
|
|||||||
FROM chat
|
FROM chat
|
||||||
WHERE stream_id = ?
|
WHERE stream_id = ?
|
||||||
ORDER BY time_sent DESC
|
ORDER BY time_sent DESC
|
||||||
LIMIT 1
|
LIMIT 50
|
||||||
)
|
)
|
||||||
ORDER BY time_sent ASC;""", (stream_id,)).fetchall()
|
ORDER BY time_sent ASC;""", (stream_id,)).fetchall()
|
||||||
db.close_connection()
|
db.close_connection()
|
||||||
|
|
||||||
# Create JSON output of chat_history to pass through NGINX proxy
|
# Create JSON output of chat_history to pass through NGINX proxy
|
||||||
chat_history = [{"chatter_id": chat[0], "message": chat[1],
|
chat_history = [{"chatter_id": chat[0], "message": chat[1], "time_sent": chat[2]} for chat in all_chats]
|
||||||
"time_sent": chat[2]} for chat in all_chats]
|
|
||||||
|
|
||||||
# Pass the chat history to the proxy
|
# Pass the chat history to the proxy
|
||||||
return jsonify({"chat_history": chat_history}), 200
|
return jsonify({"chat_history": chat_history}), 200
|
||||||
@@ -91,7 +89,19 @@ def send_chat(data) -> None:
|
|||||||
emit("error", {"error": "Unable to send a chat"}, broadcast=False)
|
emit("error", {"error": "Unable to send a chat"}, broadcast=False)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Save chat information to database so other users can see
|
# Send the chat message to the client so it can be displayed
|
||||||
|
emit("new_message", {
|
||||||
|
"chatter_id": chatter_id,
|
||||||
|
"message": message,
|
||||||
|
"time_sent": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
}, room=stream_id)
|
||||||
|
|
||||||
|
# Asynchronously save the chat
|
||||||
|
save_chat(chatter_id, stream_id, message)
|
||||||
|
|
||||||
|
|
||||||
|
def save_chat(chatter_id, stream_id, message):
|
||||||
|
"""Save the chat to the database"""
|
||||||
db = Database()
|
db = Database()
|
||||||
cursor = db.create_connection()
|
cursor = db.create_connection()
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
@@ -99,10 +109,3 @@ def send_chat(data) -> None:
|
|||||||
VALUES (?, ?, ?);""", (chatter_id, stream_id, message))
|
VALUES (?, ?, ?);""", (chatter_id, stream_id, message))
|
||||||
db.commit_data()
|
db.commit_data()
|
||||||
db.close_connection()
|
db.close_connection()
|
||||||
|
|
||||||
# Send the chat message to the client so it can be displayed
|
|
||||||
emit("new_message", {
|
|
||||||
"chatter_id": chatter_id,
|
|
||||||
"message": message,
|
|
||||||
"time_sent": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
}, room=stream_id)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user